Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Display tag items #38

Open
wants to merge 46 commits into
base: views-setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
72d36e3
views exercises solution
CarlyLanglois Apr 21, 2020
11e1699
add defualt desc value
CarlyLanglois May 5, 2020
b17bb26
create event model class
CarlyLanglois May 5, 2020
3b0f2a2
add second constructor and tostring override to event model
CarlyLanglois May 14, 2020
19b486e
refine model creation for video, replace dictionary with obj list
CarlyLanglois May 14, 2020
d0db1f4
add description property to event model
CarlyLanglois May 14, 2020
56b9ee6
add event id to class and table view
CarlyLanglois May 15, 2020
43e2c93
add data later with dict of events
CarlyLanglois May 15, 2020
f47289a
remove unused findbyid
CarlyLanglois May 15, 2020
d9a1570
add delete event action
CarlyLanglois May 15, 2020
4089ae8
Added binding attributes
gildedgardenia May 19, 2020
4d50b44
use class property names for binding
CarlyLanglois May 22, 2020
4f3c49e
remove attributes
CarlyLanglois May 22, 2020
7e16465
Merge pull request #1 from LaunchCodeEducation/model-binding-alternative
CarlyLanglois Jun 2, 2020
b254d87
Refactored controller and view
gildedgardenia Jun 8, 2020
159ef03
Refactored to use ViewModels
gildedgardenia Jun 9, 2020
f853367
Added validation attributes
gildedgardenia Jun 9, 2020
8fcd66c
Add controller logic
gildedgardenia Jun 9, 2020
ac0acbe
Refactored form
gildedgardenia Jun 9, 2020
f7dd238
Updated to use enums for event types
gildedgardenia Jun 10, 2020
4a76f8c
remove all caps from enum type, modify select list creation
CarlyLanglois Jun 21, 2020
96c4e6f
add that setter back in there
CarlyLanglois Jun 22, 2020
c449ffd
Merge pull request #3 from LaunchCodeEducation/enums-alternative
CarlyLanglois Jun 22, 2020
521b167
Code for ORM lesson 1
chrisbay Jun 22, 2020
bd96f08
Minor refactor
chrisbay Jun 24, 2020
c984c61
Adding solution to exercises
gildedgardenia Jun 25, 2020
664ebb5
Updated exercise solution
gildedgardenia Jun 26, 2020
2839867
Added studio solution
gildedgardenia Jun 26, 2020
fa3391e
post method name, constructor chaining
CarlyLanglois Jun 29, 2020
d9b63e7
add ef mysql dependency and db connection string
CarlyLanglois Jul 2, 2020
838dc50
create dbcontext store, add as service, modify event id property
CarlyLanglois Jul 3, 2020
a00fdaf
add initial migration, ef design package
CarlyLanglois Jul 5, 2020
00d8689
add migrations
CarlyLanglois Jul 6, 2020
068b37e
replace eventData use in controller with dbcontext object
CarlyLanglois Jul 6, 2020
39755b2
Merges Studio 16 solution
chrisbay Jul 8, 2020
1cc4fd8
Establishes one-to-many relationship
chrisbay Jul 9, 2020
5c43c73
Refactors controller
chrisbay Jul 9, 2020
2736164
Creates db migration
chrisbay Jul 9, 2020
4db6d64
Adds Event Detail view
chrisbay Jul 9, 2020
952565e
Adds Tag Model
chrisbay Jul 9, 2020
de58ed0
Creates EventTag join class
chrisbay Jul 9, 2020
02e0d98
Creates /Tag/AddEvent/X view/path
chrisbay Jul 9, 2020
3e85075
Adds tags to event detail view
chrisbay Jul 9, 2020
84ade8b
Prevents duplicate tag error
chrisbay Jul 9, 2020
b79cfd4
Display events by tag
chrisbay Jul 9, 2020
3933dfd
updated dotnet -> 6, nuget updated, startup.cs updated
speudusa Nov 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion CodingEventsDemo/CodingEventsDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
</PropertyGroup>



<ItemGroup>
<Folder Include="Views\Events\" />
<Folder Include="Data\" />
<Folder Include="ViewModels\" />
<Folder Include="Views\EventCategory\" />
<Folder Include="Views\Tag\" />
</ItemGroup>
<ItemGroup>
<None Remove="Pomelo.EntityFrameworkCore.MySql" />
<None Remove="Microsoft.EntityFrameworkCore.Design" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.11">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
57 changes: 57 additions & 0 deletions CodingEventsDemo/Controllers/EventCategoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodingEventsDemo.Data;
using CodingEventsDemo.Models;
using Microsoft.AspNetCore.Mvc;
using CodingEventsDemo.ViewModels;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CodingEventsDemo.Controllers
{
public class EventCategoryController : Controller
{
private EventDbContext context;

public EventCategoryController(EventDbContext dbContext)
{
context = dbContext;
}

// GET: /<controller>/
[HttpGet]
public IActionResult Index()
{
List<EventCategory> categories = context.Categories.ToList();
return View(categories);
}

[HttpGet]
public IActionResult Create()
{
AddEventCategoryViewModel addEventCategoryViewModel = new AddEventCategoryViewModel();
return View(addEventCategoryViewModel);
}

[HttpPost]
public IActionResult ProcessCreateEventCategoryForm(AddEventCategoryViewModel addEventCategoryViewModel)
{
if (ModelState.IsValid)
{
EventCategory newCategory = new EventCategory
{
Name = addEventCategoryViewModel.Name
};

context.Categories.Add(newCategory);
context.SaveChanges();

return Redirect("/EventCategory");
}

return View("Create", addEventCategoryViewModel);
}
}
}
76 changes: 70 additions & 6 deletions CodingEventsDemo/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodingEventsDemo.Data;
using CodingEventsDemo.Models;
using CodingEventsDemo.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -11,28 +15,88 @@ namespace coding_events_practice.Controllers
public class EventsController : Controller
{

static private List<string> Events = new List<string>();
private EventDbContext context;

public EventsController(EventDbContext dbContext)
{
context = dbContext;
}

// GET: /<controller>/
public IActionResult Index()
{
ViewBag.events = Events;
List<Event> events = context.Events
.Include(e => e.Category)
.ToList();

return View();
return View(events);
}

public IActionResult Add()
{
List<EventCategory> categories = context.Categories.ToList();
AddEventViewModel addEventViewModel = new AddEventViewModel(categories);

return View(addEventViewModel);
}

[HttpPost]
public IActionResult Add(AddEventViewModel addEventViewModel)
{
if (ModelState.IsValid)
{
EventCategory theCategory = context.Categories.Find(addEventViewModel.CategoryId);
Event newEvent = new Event
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail,
Category = theCategory
};

context.Events.Add(newEvent);
context.SaveChanges();

return Redirect("/Events");
}

return View(addEventViewModel);
}

public IActionResult Delete()
{
ViewBag.events = context.Events.ToList();

return View();
}

[HttpPost]
[Route("Events/Add")]
public IActionResult NewEvent(string name)
public IActionResult Delete(int[] eventIds)
{
Events.Add(name);
foreach (int eventId in eventIds)
{
Event theEvent = context.Events.Find(eventId);
context.Events.Remove(theEvent);
}

context.SaveChanges();

return Redirect("/Events");
}

public IActionResult Detail(int id)
{
Event theEvent = context.Events
.Include(e => e.Category)
.Single(e => e.Id == id);

List<EventTag> eventTags = context.EventTags
.Where(et => et.EventId == id)
.Include(et => et.Tag)
.ToList();

EventDetailViewModel viewModel = new EventDetailViewModel(theEvent, eventTags);
return View(viewModel);
}
}
}
103 changes: 103 additions & 0 deletions CodingEventsDemo/Controllers/TagController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodingEventsDemo.Data;
using CodingEventsDemo.Models;
using CodingEventsDemo.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CodingEventsDemo.Controllers
{
public class TagController : Controller
{
private EventDbContext context;

public TagController(EventDbContext dbContext)
{
context = dbContext;
}

// GET: /<controller>/
public IActionResult Index()
{
List<Tag> tags = context.Tags.ToList();
return View(tags);
}

public IActionResult Add()
{
Tag tag = new Tag();
return View(tag);
}

[HttpPost]
public IActionResult Add(Tag tag)
{
if (ModelState.IsValid)
{
context.Tags.Add(tag);
context.SaveChanges();
return Redirect("/Tag/");
}

return View("Add", tag);
}

public IActionResult AddEvent(int id)
{
Event theEvent = context.Events.Find(id);
List<Tag> possibleTags = context.Tags.ToList();

AddEventTagViewModel viewModel = new AddEventTagViewModel(theEvent, possibleTags);

return View(viewModel);
}

[HttpPost]
public IActionResult AddEvent(AddEventTagViewModel viewModel)
{
if (ModelState.IsValid)
{
int eventId = viewModel.EventId;
int tagId = viewModel.TagId;

List<EventTag> existingItems = context.EventTags
.Where(et => et.EventId == eventId)
.Where(et => et.TagId == tagId)
.ToList();

if (existingItems.Count == 0)
{

EventTag eventTag = new EventTag
{
EventId = eventId,
TagId = tagId
};

context.EventTags.Add(eventTag);
context.SaveChanges();
}

return Redirect("/Events/Detail/" + eventId);
}

return View(viewModel);
}

public IActionResult Detail(int id)
{
List<EventTag> eventTags = context.EventTags
.Where(et => et.TagId == id)
.Include(et => et.Event)
.Include(et => et.Tag)
.ToList();

return View(eventTags);
}
}
}
23 changes: 23 additions & 0 deletions CodingEventsDemo/Data/EventDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CodingEventsDemo.Models;
using Microsoft.EntityFrameworkCore;

namespace CodingEventsDemo.Data
{
public class EventDbContext : DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<EventCategory> Categories { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<EventTag> EventTags { get; set; }

public EventDbContext(DbContextOptions<EventDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EventTag>().HasKey(et => new { et.EventId, et.TagId });
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading