Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit ce32d78

Browse files
davidfowlDamianEdwards
authored andcommitted
Added more save points
1 parent d6191e3 commit ce32d78

38 files changed

+931
-832
lines changed

save-points/2a-Refactor-to-ConferenceDTO/ConferencePlanner/BackEnd/BackEnd.csproj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
6-
</PropertyGroup>
7-
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
</PropertyGroup>
86
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.App" />
10-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.1" />
12-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
13-
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
7+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4" />
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.4">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
</PackageReference>
13+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0-preview5-19264-04">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
1418
</ItemGroup>
15-
19+
1620
<ItemGroup>
1721
<ProjectReference Include="..\ConferenceDTO\ConferenceDTO.csproj" />
1822
</ItemGroup>
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using BackEnd.Data;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.EntityFrameworkCore;
7+
using Microsoft.AspNetCore.Http;
8+
using ConferenceDTO;
9+
10+
namespace BackEnd.Controllers
11+
{
12+
[Route("/api/[controller]")]
13+
[ApiController]
14+
public class AttendeesController : ControllerBase
15+
{
16+
private readonly ApplicationDbContext _context;
17+
18+
public AttendeesController(ApplicationDbContext context)
19+
{
20+
_context = context;
21+
}
22+
23+
[HttpGet("{username}")]
24+
public async Task<ActionResult<AttendeeResponse>> Get(string username)
25+
{
26+
var attendee = await _context.Attendees.Include(a => a.SessionsAttendees)
27+
.ThenInclude(sa => sa.Session)
28+
.SingleOrDefaultAsync(a => a.UserName == username);
29+
30+
if (attendee == null)
31+
{
32+
return NotFound();
33+
}
34+
35+
var result = attendee.MapAttendeeResponse();
36+
37+
return result;
38+
}
39+
40+
[HttpPost]
41+
[ProducesResponseType(StatusCodes.Status201Created)]
42+
[ProducesResponseType(StatusCodes.Status409Conflict)]
43+
public async Task<ActionResult<AttendeeResponse>> Post(ConferenceDTO.Attendee input)
44+
{
45+
// Check if the attendee already exists
46+
var existingAttendee = await _context.Attendees
47+
.Where(a => a.UserName == input.UserName)
48+
.FirstOrDefaultAsync();
49+
50+
if (existingAttendee != null)
51+
{
52+
return Conflict(input);
53+
}
54+
55+
var attendee = new Data.Attendee
56+
{
57+
FirstName = input.FirstName,
58+
LastName = input.LastName,
59+
UserName = input.UserName,
60+
EmailAddress = input.EmailAddress
61+
};
62+
63+
_context.Attendees.Add(attendee);
64+
await _context.SaveChangesAsync();
65+
66+
var result = attendee.MapAttendeeResponse();
67+
68+
return CreatedAtAction(nameof(Get), new { username = result.UserName }, result);
69+
}
70+
71+
[HttpPost("{username}/session/{sessionId}")]
72+
[ProducesResponseType(StatusCodes.Status200OK)]
73+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
74+
[ProducesResponseType(StatusCodes.Status404NotFound)]
75+
[ProducesDefaultResponseType]
76+
public async Task<ActionResult<AttendeeResponse>> AddSession(string username, int sessionId)
77+
{
78+
var attendee = await _context.Attendees.Include(a => a.SessionsAttendees)
79+
.ThenInclude(sa => sa.Session)
80+
.SingleOrDefaultAsync(a => a.UserName == username);
81+
82+
if (attendee == null)
83+
{
84+
return NotFound();
85+
}
86+
87+
var session = await _context.Sessions.FindAsync(sessionId);
88+
89+
if (session == null)
90+
{
91+
return BadRequest();
92+
}
93+
94+
attendee.SessionsAttendees.Add(new SessionAttendee
95+
{
96+
AttendeeId = attendee.Id,
97+
SessionId = sessionId
98+
});
99+
100+
await _context.SaveChangesAsync();
101+
102+
var result = attendee.MapAttendeeResponse();
103+
104+
return result;
105+
}
106+
107+
[HttpDelete("{username}/session/{sessionId}")]
108+
[ProducesResponseType(StatusCodes.Status204NoContent)]
109+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
110+
[ProducesResponseType(StatusCodes.Status404NotFound)]
111+
[ProducesDefaultResponseType]
112+
public async Task<IActionResult> RemoveSession(string username, int sessionId)
113+
{
114+
var attendee = await _context.Attendees.Include(a => a.SessionsAttendees)
115+
.SingleOrDefaultAsync(a => a.UserName == username);
116+
117+
if (attendee == null)
118+
{
119+
return NotFound();
120+
}
121+
122+
var session = await _context.Sessions.FindAsync(sessionId);
123+
124+
if (session == null)
125+
{
126+
return BadRequest();
127+
}
128+
129+
var sessionAttendee = attendee.SessionsAttendees.FirstOrDefault(sa => sa.SessionId == sessionId);
130+
attendee.SessionsAttendees.Remove(sessionAttendee);
131+
132+
await _context.SaveChangesAsync();
133+
134+
return NoContent();
135+
}
136+
}
137+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using BackEnd.Data;
6+
using ConferenceDTO;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace BackEnd.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class SessionsController : Controller
16+
{
17+
private readonly ApplicationDbContext _context;
18+
19+
public SessionsController(ApplicationDbContext context)
20+
{
21+
_context = context;
22+
}
23+
24+
[HttpGet]
25+
public async Task<ActionResult<List<SessionResponse>>> Get()
26+
{
27+
var sessions = await _context.Sessions.AsNoTracking()
28+
.Include(s => s.Track)
29+
.Include(s => s.SessionSpeakers)
30+
.ThenInclude(ss => ss.Speaker)
31+
.Select(m => m.MapSessionResponse())
32+
.ToListAsync();
33+
return sessions;
34+
}
35+
36+
[HttpGet("{id}")]
37+
public async Task<ActionResult<SessionResponse>> Get(int id)
38+
{
39+
var session = await _context.Sessions.AsNoTracking()
40+
.Include(s => s.Track)
41+
.Include(s => s.SessionSpeakers)
42+
.ThenInclude(ss => ss.Speaker)
43+
.SingleOrDefaultAsync(s => s.Id == id);
44+
45+
if (session == null)
46+
{
47+
return NotFound();
48+
}
49+
50+
return session.MapSessionResponse();
51+
}
52+
53+
[HttpPost]
54+
public async Task<ActionResult<SessionResponse>> Post(ConferenceDTO.Session input)
55+
{
56+
var session = new Data.Session
57+
{
58+
Title = input.Title,
59+
StartTime = input.StartTime,
60+
EndTime = input.EndTime,
61+
Abstract = input.Abstract,
62+
TrackId = input.TrackId
63+
};
64+
65+
_context.Sessions.Add(session);
66+
await _context.SaveChangesAsync();
67+
68+
var result = session.MapSessionResponse();
69+
70+
return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
71+
}
72+
73+
[HttpPut("{id}")]
74+
public async Task<IActionResult> Put(int id, ConferenceDTO.Session input)
75+
{
76+
var session = await _context.Sessions.FindAsync(id);
77+
78+
if (session == null)
79+
{
80+
return NotFound();
81+
}
82+
83+
session.Id = input.Id;
84+
session.Title = input.Title;
85+
session.Abstract = input.Abstract;
86+
session.StartTime = input.StartTime;
87+
session.EndTime = input.EndTime;
88+
session.TrackId = input.TrackId;
89+
90+
await _context.SaveChangesAsync();
91+
92+
return NoContent();
93+
}
94+
95+
[HttpDelete("{id}")]
96+
public async Task<ActionResult<SessionResponse>> Delete(int id)
97+
{
98+
var session = await _context.Sessions.FindAsync(id);
99+
100+
if (session == null)
101+
{
102+
return NotFound();
103+
}
104+
105+
_context.Sessions.Remove(session);
106+
await _context.SaveChangesAsync();
107+
108+
return session.MapSessionResponse();
109+
}
110+
111+
112+
[HttpPost("upload")]
113+
[Consumes("multipart/form-data")]
114+
public async Task<IActionResult> Upload([FromForm]ConferenceFormat format, IFormFile file)
115+
{
116+
var loader = GetLoader(format);
117+
118+
using (var stream = file.OpenReadStream())
119+
{
120+
await loader.LoadDataAsync(stream, _context);
121+
}
122+
123+
await _context.SaveChangesAsync();
124+
125+
return Ok();
126+
}
127+
128+
private static DataLoader GetLoader(ConferenceFormat format)
129+
{
130+
if (format == ConferenceFormat.Sessionize)
131+
{
132+
return new SessionizeLoader();
133+
}
134+
return new DevIntersectionLoader();
135+
}
136+
137+
public enum ConferenceFormat
138+
{
139+
Sessionize,
140+
DevIntersections
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)