7
7
using Microsoft . AspNetCore . Http ;
8
8
using ConferenceDTO ;
9
9
10
- namespace BackEnd
10
+ namespace BackEnd . Controllers
11
11
{
12
12
[ Route ( "/api/[controller]" ) ]
13
13
[ ApiController ]
14
14
public class AttendeesController : ControllerBase
15
15
{
16
- private readonly ApplicationDbContext _db ;
16
+ private readonly ApplicationDbContext _context ;
17
17
18
- public AttendeesController ( ApplicationDbContext db )
18
+ public AttendeesController ( ApplicationDbContext context )
19
19
{
20
- _db = db ;
20
+ _context = context ;
21
21
}
22
22
23
- [ HttpGet ( "{id }" ) ]
24
- public async Task < ActionResult < AttendeeResponse > > Get ( string id )
23
+ [ HttpGet ( "{username }" ) ]
24
+ public async Task < ActionResult < AttendeeResponse > > Get ( string username )
25
25
{
26
- var attendee = await _db . Attendees . Include ( a => a . SessionsAttendees )
26
+ var attendee = await _context . Attendees . Include ( a => a . SessionsAttendees )
27
27
. ThenInclude ( sa => sa . Session )
28
- . SingleOrDefaultAsync ( a => a . UserName == id ) ;
28
+ . SingleOrDefaultAsync ( a => a . UserName == username ) ;
29
29
30
30
if ( attendee == null )
31
31
{
@@ -37,13 +37,26 @@ public async Task<ActionResult<AttendeeResponse>> Get(string id)
37
37
return result ;
38
38
}
39
39
40
+ [ HttpGet ( "{username}/sessions" ) ]
41
+ public async Task < ActionResult < List < SessionResponse > > > GetSessions ( string username )
42
+ {
43
+ var sessions = await _context . Sessions . AsNoTracking ( )
44
+ . Include ( s => s . Track )
45
+ . Include ( s => s . SessionSpeakers )
46
+ . ThenInclude ( ss => ss . Speaker )
47
+ . Where ( s => s . SessionAttendees . Any ( sa => sa . Attendee . UserName == username ) )
48
+ . Select ( m => m . MapSessionResponse ( ) )
49
+ . ToListAsync ( ) ;
50
+ return sessions ;
51
+ }
52
+
40
53
[ HttpPost ]
41
54
[ ProducesResponseType ( StatusCodes . Status201Created ) ]
42
55
[ ProducesResponseType ( StatusCodes . Status409Conflict ) ]
43
56
public async Task < ActionResult < AttendeeResponse > > Post ( ConferenceDTO . Attendee input )
44
57
{
45
58
// Check if the attendee already exists
46
- var existingAttendee = await _db . Attendees
59
+ var existingAttendee = await _context . Attendees
47
60
. Where ( a => a . UserName == input . UserName )
48
61
. FirstOrDefaultAsync ( ) ;
49
62
@@ -60,33 +73,31 @@ public async Task<ActionResult<AttendeeResponse>> Post(ConferenceDTO.Attendee in
60
73
EmailAddress = input . EmailAddress
61
74
} ;
62
75
63
- _db . Attendees . Add ( attendee ) ;
64
- await _db . SaveChangesAsync ( ) ;
76
+ _context . Attendees . Add ( attendee ) ;
77
+ await _context . SaveChangesAsync ( ) ;
65
78
66
79
var result = attendee . MapAttendeeResponse ( ) ;
67
80
68
- return CreatedAtAction ( nameof ( Get ) , new { id = result . UserName } , result ) ;
81
+ return CreatedAtAction ( nameof ( Get ) , new { username = result . UserName } , result ) ;
69
82
}
70
83
71
- [ HttpPost ( "{username}/session/{sessionId:int }" ) ]
84
+ [ HttpPost ( "{username}/session/{sessionId}" ) ]
72
85
[ ProducesResponseType ( StatusCodes . Status200OK ) ]
73
86
[ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
74
87
[ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
75
88
[ ProducesDefaultResponseType ]
76
89
public async Task < ActionResult < AttendeeResponse > > AddSession ( string username , int sessionId )
77
90
{
78
- var attendee = await _db . Attendees . Include ( a => a . SessionsAttendees )
91
+ var attendee = await _context . Attendees . Include ( a => a . SessionsAttendees )
79
92
. ThenInclude ( sa => sa . Session )
80
- . Include ( a => a . ConferenceAttendees )
81
- . ThenInclude ( ca => ca . Conference )
82
93
. SingleOrDefaultAsync ( a => a . UserName == username ) ;
83
94
84
95
if ( attendee == null )
85
96
{
86
97
return NotFound ( ) ;
87
98
}
88
99
89
- var session = await _db . Sessions . FindAsync ( sessionId ) ;
100
+ var session = await _context . Sessions . FindAsync ( sessionId ) ;
90
101
91
102
if ( session == null )
92
103
{
@@ -95,45 +106,45 @@ public async Task<ActionResult<AttendeeResponse>> AddSession(string username, in
95
106
96
107
attendee . SessionsAttendees . Add ( new SessionAttendee
97
108
{
98
- AttendeeID = attendee . ID ,
99
- SessionID = sessionId
109
+ AttendeeId = attendee . Id ,
110
+ SessionId = sessionId
100
111
} ) ;
101
112
102
- await _db . SaveChangesAsync ( ) ;
113
+ await _context . SaveChangesAsync ( ) ;
103
114
104
115
var result = attendee . MapAttendeeResponse ( ) ;
105
116
106
117
return result ;
107
118
}
108
119
109
- [ HttpDelete ( "{username}/session/{sessionId:int }" ) ]
120
+ [ HttpDelete ( "{username}/session/{sessionId}" ) ]
110
121
[ ProducesResponseType ( StatusCodes . Status204NoContent ) ]
111
122
[ ProducesResponseType ( StatusCodes . Status400BadRequest ) ]
112
123
[ ProducesResponseType ( StatusCodes . Status404NotFound ) ]
113
124
[ ProducesDefaultResponseType ]
114
125
public async Task < IActionResult > RemoveSession ( string username , int sessionId )
115
126
{
116
- var attendee = await _db . Attendees . Include ( a => a . SessionsAttendees )
127
+ var attendee = await _context . Attendees . Include ( a => a . SessionsAttendees )
117
128
. SingleOrDefaultAsync ( a => a . UserName == username ) ;
118
129
119
130
if ( attendee == null )
120
131
{
121
132
return NotFound ( ) ;
122
133
}
123
134
124
- var session = await _db . Sessions . FindAsync ( sessionId ) ;
135
+ var session = await _context . Sessions . FindAsync ( sessionId ) ;
125
136
126
137
if ( session == null )
127
138
{
128
139
return BadRequest ( ) ;
129
140
}
130
141
131
- var sessionAttendee = attendee . SessionsAttendees . FirstOrDefault ( sa => sa . SessionID == sessionId ) ;
142
+ var sessionAttendee = attendee . SessionsAttendees . FirstOrDefault ( sa => sa . SessionId == sessionId ) ;
132
143
attendee . SessionsAttendees . Remove ( sessionAttendee ) ;
133
144
134
- await _db . SaveChangesAsync ( ) ;
145
+ await _context . SaveChangesAsync ( ) ;
135
146
136
147
return NoContent ( ) ;
137
148
}
138
149
}
139
- }
150
+ }
0 commit comments