This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathTagController.cs
More file actions
103 lines (84 loc) · 2.71 KB
/
TagController.cs
File metadata and controls
103 lines (84 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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);
}
}
}