forked from jongalloway/aspnetcore-app-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttendeesController.cs
123 lines (96 loc) · 3.58 KB
/
AttendeesController.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System.Collections.Generic;
using System.Threading.Tasks;
using BackEnd.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace BackEnd
{
[Route("/api/[controller]")]
public class AttendeesController : Controller
{
private readonly ApplicationDbContext _db;
public AttendeesController(ApplicationDbContext db)
{
_db = db;
}
[HttpGet("{username}")]
public async Task<IActionResult> Get(string username)
{
var attendee = await _db.Attendees.Include(a => a.SessionsAttendees)
.ThenInclude(sa => sa.Session)
.SingleOrDefaultAsync(a => a.UserName == username);
if (attendee == null)
{
return NotFound();
}
var result = attendee.MapAttendeeResponse();
return Ok(result);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]ConferenceDTO.Attendee input)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var attendee = new Attendee
{
FirstName = input.FirstName,
LastName = input.LastName,
UserName = input.UserName
};
_db.Attendees.Add(attendee);
await _db.SaveChangesAsync();
var result = attendee.MapAttendeeResponse();
return CreatedAtAction(nameof(Get), new { username = result.UserName }, result);
}
[HttpPost("{username}/session/{sessionId:int}")]
public async Task<IActionResult> AddSession(string username, int sessionId)
{
var attendee = await _db.Attendees.Include(a => a.SessionsAttendees)
.ThenInclude(sa => sa.Session)
.Include(a => a.ConferenceAttendees)
.ThenInclude(ca => ca.Conference)
.SingleOrDefaultAsync(a => a.UserName == username);
if (attendee == null)
{
return NotFound();
}
var session = await _db.Sessions.FindAsync(sessionId);
if (session == null)
{
return BadRequest();
}
attendee.SessionsAttendees.Add(new SessionAttendee
{
AttendeeID = attendee.ID,
SessionID = sessionId
});
await _db.SaveChangesAsync();
var result = attendee.MapAttendeeResponse();
return Ok(result);
}
[HttpDelete("{username}/session/{sessionId:int}")]
public async Task<IActionResult> RemoveSession(string username, int sessionId)
{
var attendee = await _db.Attendees.SingleOrDefaultAsync(a => a.UserName == username);
if (attendee == null)
{
return NotFound();
}
var session = await _db.Sessions.FindAsync(sessionId);
if (session == null)
{
return BadRequest();
}
attendee.SessionsAttendees.Remove(new SessionAttendee
{
AttendeeID = attendee.ID,
SessionID = sessionId
});
await _db.SaveChangesAsync();
var result = attendee.MapAttendeeResponse();
return NoContent();
}
}
}