diff --git a/WebApplication1/WebApplication1/Controllers/Saloon.cs b/WebApplication1/WebApplication1/Controllers/Saloon.cs index 64350c3..9cc70f8 100644 --- a/WebApplication1/WebApplication1/Controllers/Saloon.cs +++ b/WebApplication1/WebApplication1/Controllers/Saloon.cs @@ -17,6 +17,7 @@ public class SaloonCreateRequest [HttpPost("/api/saloon")] [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public IActionResult Create(SaloonCreateRequest req) { @@ -32,7 +33,10 @@ public IActionResult Create(SaloonCreateRequest req) .FirstOrDefault(u => u.UserId == req.UserId); if (usr == null) - return Unauthorized(new { Status = 401, Message = $"Unauthorized" }); + return Unauthorized(new { Status = 401, Message = "Unauthorized" }); + + if (ctx.Saloons.Any(s => s.Name == req.Name)) + return BadRequest(new { Status = 400, Message = "A saloon with this name already exists!" }); Saloon newSaloon = new Saloon() { diff --git a/WebApplication1/WebApplication1/Controllers/Workshop.cs b/WebApplication1/WebApplication1/Controllers/Workshop.cs index b22e2ca..1232465 100644 --- a/WebApplication1/WebApplication1/Controllers/Workshop.cs +++ b/WebApplication1/WebApplication1/Controllers/Workshop.cs @@ -13,19 +13,27 @@ public class WorkshopController : ControllerBase { [HttpGet("/api/workshop")] [ProducesResponseType(StatusCodes.Status200OK)] - public IActionResult GetList() + public IActionResult GetList([FromQuery] int? status, [FromQuery] int? userid) { - List marshalled = new List(); - using (Ws1Context ws1Context = new Ws1Context()) + using (Ws1Context ctx = new Ws1Context()) { - foreach (Workshop w in ws1Context.Workshops.Include(w => w.Category)) - marshalled.Add(Marshall.Json(w, 1)); + IQueryable query = ctx.Workshops + .Include(w => w.Category) + .Include(w => w.Saloon); + + if (status != null) + query = query.Where(w => w.Status == status); + if (userid != null) + query = query.Where(w => w.UserId == userid); + + return Ok(new + { + Status = 200, + Message = "Successfully fetched workshops!", + Data = query + .Select(w => Marshall.Json(w, 1)) + .ToList(), + }); } - return Ok(new - { - Status = 200, - Message = "Successfully fetched workshops!", - Data = marshalled, - }); } } diff --git a/WebApplication1/WebApplication1/Controllers/WorkshopRequest.cs b/WebApplication1/WebApplication1/Controllers/WorkshopRequest.cs index 39171a6..48b599d 100644 --- a/WebApplication1/WebApplication1/Controllers/WorkshopRequest.cs +++ b/WebApplication1/WebApplication1/Controllers/WorkshopRequest.cs @@ -27,7 +27,7 @@ public class WorkshopRequestCreateRequest /// public int Timeslot { get; set; } public string Category { get; set; } = null!; - public int SaloonId { get; set; } + public string Saloon { get; set; } = null!; } [HttpPost("/api/workshop-request")] @@ -61,10 +61,10 @@ public IActionResult Create(WorkshopRequestCreateRequest req) // Check for valid saloonID Saloon? saloon = ctx.Saloons .Include(s => s.Workshops) - .FirstOrDefault(s => s.SaloonId == req.SaloonId); + .FirstOrDefault(s => s.Name == req.Saloon); if (saloon == null) - return BadRequest(new { Status = 400, Message = $"SaloonID {req.SaloonId} does not exist" }); + return BadRequest(new { Status = 400, Message = $"Saloon {req.Saloon} does not exist" }); if (saloon.Workshops.Any(w => w.Status == 1 && Time.Overlaps(w.StartDate, w.EndDate, req.StartDate, req.EndDate) @@ -86,6 +86,8 @@ public IActionResult Create(WorkshopRequestCreateRequest req) WorkshopId = ctx.Workshops.Count(), Title = req.Title, Timeslot = req.Timeslot, + StartDate = req.StartDate, + EndDate = req.EndDate, Category = cat }; diff --git a/WebApplication1/WebApplication1/Utils/Marshall.cs b/WebApplication1/WebApplication1/Utils/Marshall.cs index ac8debc..67e0716 100644 --- a/WebApplication1/WebApplication1/Utils/Marshall.cs +++ b/WebApplication1/WebApplication1/Utils/Marshall.cs @@ -28,6 +28,8 @@ public static class Marshall StartDate = w.StartDate, Timeslot = Timeslot.AsString(w.Timeslot), Title = w.Title, + Status = w.Status == 0 ? "Pending" : (w.Status == 1 ? "Accepted" : "Rejected"), + RequestDate = w.RequestDate, Category = Json(w.Category, depth - 1), Saloon = Json(w.Saloon, depth - 1), };