From 8a26b957aa552e831b35f240db98bdda0f920f7a Mon Sep 17 00:00:00 2001 From: Niggl Date: Thu, 18 Jun 2020 15:10:42 +0200 Subject: [PATCH] Added filters for all get-all endpoints --- .../Controllers/CategoriesController.cs | 21 +- .../Controllers/ProjectsController.cs | 15 +- .../Controllers/SprintsController.cs | 183 ++++++++++-------- .../Controllers/StatusController.cs | 8 +- ScrumTaskboard/Controllers/TasksController.cs | 38 +++- ScrumTaskboard/Controllers/UsersController.cs | 8 +- .../Controllers/UserstoriesController.cs | 38 +++- 7 files changed, 219 insertions(+), 92 deletions(-) diff --git a/ScrumTaskboard/Controllers/CategoriesController.cs b/ScrumTaskboard/Controllers/CategoriesController.cs index 2b1afbe..7cffa97 100644 --- a/ScrumTaskboard/Controllers/CategoriesController.cs +++ b/ScrumTaskboard/Controllers/CategoriesController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,7 +22,23 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetCategory() { - return await _context.Categories.ToListAsync(); + IEnumerable filtered = _context.Categories; + if (HttpContext.Request.Query["title"].ToString() != "") + { + filtered = filtered.Where(c => c.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + if (Convert.ToInt32(HttpContext.Request.Query["projectid"]) != 0) + { + filtered = filtered.Where(c => c.projectid == Convert.ToInt32(HttpContext.Request.Query["projectid"])); + } + if (HttpContext.Request.Query["color"].ToString() != "") + { + filtered = filtered.Where(c => c.color == HttpContext.Request.Query["color"].ToString()); + + } + + + return filtered.ToList(); } // GET: api/category/1 diff --git a/ScrumTaskboard/Controllers/ProjectsController.cs b/ScrumTaskboard/Controllers/ProjectsController.cs index c4c4d19..3092647 100644 --- a/ScrumTaskboard/Controllers/ProjectsController.cs +++ b/ScrumTaskboard/Controllers/ProjectsController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,7 +22,17 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetProject() { - return await _context.Projects.ToListAsync(); + IEnumerable filtered = _context.Projects; + if (HttpContext.Request.Query["title"].ToString() != "") + { + filtered = filtered.Where(t => t.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + if (HttpContext.Request.Query["isprivate"].ToString() != "") + { + filtered = filtered.Where(t => t.isprivate == Convert.ToBoolean(HttpContext.Request.Query["isprivate"])); + } + + return filtered.ToList(); } // GET: api/projects/5 diff --git a/ScrumTaskboard/Controllers/SprintsController.cs b/ScrumTaskboard/Controllers/SprintsController.cs index 288d995..45fe7af 100644 --- a/ScrumTaskboard/Controllers/SprintsController.cs +++ b/ScrumTaskboard/Controllers/SprintsController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,92 +22,110 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetSprint() { - return await _context.Sprints.ToListAsync(); - } - - // GET: api/sprint/5 - [HttpGet("{id}")] - public async Task> GetSprint(int id) - { - var Sprint = await _context.Sprints.FindAsync(id); - - if (Sprint == null) + IEnumerable filtered = _context.Sprints; + if (HttpContext.Request.Query["title"].ToString() != "") { - return NotFound(); + filtered = filtered.Where(s => s.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + if (Convert.ToInt32(HttpContext.Request.Query["projectid"]) != 0) + { + filtered = filtered.Where(s => s.projectid == Convert.ToInt32(HttpContext.Request.Query["projectid"])); + } + if (HttpContext.Request.Query["startDate"].ToString() != "") + { + filtered = filtered.Where(s => s.startDate == DateTime.Parse(HttpContext.Request.Query["startDate"].ToString())); + } + if (HttpContext.Request.Query["endDate"].ToString() != "") + { + filtered = filtered.Where(s => s.endDate == DateTime.Parse(HttpContext.Request.Query["startDate"].ToString())); } - return Sprint; - } - - // PUT: api/sprint/5 - [HttpPut("{id}")] - public async Task PutSprint(int id, ScrumSprint sprint) - { - // Die ID des Sprints darf nicht geändert werden - if (id != sprint.id) - { - return BadRequest(); - } - - // Speichere den geänderten Sprint im Context - _context.Entry(sprint).State = EntityState.Modified; - - try - { - // Übernehme die Änderungen in die Datenbank - await _context.SaveChangesAsync(); - } - catch (DbUpdateConcurrencyException) - { - // Wenn der Sprint nicht existiert, gib Statuscode 404 zurück - if (!SprintExists(id)) - { - return NotFound(); - } - else - { - throw; - } - } - - return NoContent(); - } - - // POST: api/Sprint - [HttpPost] - public async Task> PostSprint(ScrumSprint sprint) - { - _context.Sprints.Add(sprint); - await _context.SaveChangesAsync(); - - // Der neue Sprint wurde erstellt und kann über die GetSprint Methode abgerufen werden. - return CreatedAtAction("GetSprint", new { id = sprint.id }, sprint); - } - - // DELETE: api/Sprint/5 - [HttpDelete("{id}")] - public async Task> DeleteSprint(int id) - { - var scrumSprint = await _context.Sprints.FindAsync(id); - if (scrumSprint == null) - { - return NotFound(); - } - - // Entferne den Sprint aus dem Context - _context.Sprints.Remove(scrumSprint); - // Speichere die Änderungen in der Datenbank - await _context.SaveChangesAsync(); - - return scrumSprint; - } - + return filtered.ToList(); + } + + // GET: api/sprint/5 + [HttpGet("{id}")] + public async Task> GetSprint(int id) + { + var Sprint = await _context.Sprints.FindAsync(id); + + if (Sprint == null) + { + return NotFound(); + } + + return Sprint; + } + + // PUT: api/sprint/5 + [HttpPut("{id}")] + public async Task PutSprint(int id, ScrumSprint sprint) + { + // Die ID des Sprints darf nicht geändert werden + if (id != sprint.id) + { + return BadRequest(); + } + + // Speichere den geänderten Sprint im Context + _context.Entry(sprint).State = EntityState.Modified; + + try + { + // Übernehme die Änderungen in die Datenbank + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + // Wenn der Sprint nicht existiert, gib Statuscode 404 zurück + if (!SprintExists(id)) + { + return NotFound(); + } + else + { + throw; + } + } + + return NoContent(); + } + + // POST: api/Sprint + [HttpPost] + public async Task> PostSprint(ScrumSprint sprint) + { + _context.Sprints.Add(sprint); + await _context.SaveChangesAsync(); + + // Der neue Sprint wurde erstellt und kann über die GetSprint Methode abgerufen werden. + return CreatedAtAction("GetSprint", new { id = sprint.id }, sprint); + } + + // DELETE: api/Sprint/5 + [HttpDelete("{id}")] + public async Task> DeleteSprint(int id) + { + var scrumSprint = await _context.Sprints.FindAsync(id); + if (scrumSprint == null) + { + return NotFound(); + } + + // Entferne den Sprint aus dem Context + _context.Sprints.Remove(scrumSprint); + // Speichere die Änderungen in der Datenbank + await _context.SaveChangesAsync(); + + return scrumSprint; + } + /// /// Prüft, ob ein Sprint mit der angegebenen ID bereits existiert. /// - private bool SprintExists(int id) - { - return _context.Sprints.Any(e => e.id == id); - } + private bool SprintExists(int id) + { + return _context.Sprints.Any(e => e.id == id); + } } } diff --git a/ScrumTaskboard/Controllers/StatusController.cs b/ScrumTaskboard/Controllers/StatusController.cs index 30fa37c..d695715 100644 --- a/ScrumTaskboard/Controllers/StatusController.cs +++ b/ScrumTaskboard/Controllers/StatusController.cs @@ -21,7 +21,13 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetStatus() { - return await _context.Status.ToListAsync(); + IEnumerable filtered = _context.Status; + if (HttpContext.Request.Query["title"].ToString() != "") + { + filtered = filtered.Where(s => s.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + + return filtered.ToList(); } // GET: api/status/1 diff --git a/ScrumTaskboard/Controllers/TasksController.cs b/ScrumTaskboard/Controllers/TasksController.cs index 805a7c1..9f54b97 100644 --- a/ScrumTaskboard/Controllers/TasksController.cs +++ b/ScrumTaskboard/Controllers/TasksController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,7 +22,40 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetTasks() { - return await _context.Tasks.ToListAsync(); + IEnumerable filtered = _context.Tasks; + if (HttpContext.Request.Query["title"].ToString() != "") + { + filtered = filtered.Where(t => t.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + if (Convert.ToInt32(HttpContext.Request.Query["userstoryid"]) != 0) + { + filtered = filtered.Where(t => t.userstoryid == Convert.ToInt32(HttpContext.Request.Query["userstoryid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["statusid"]) != 0) + { + filtered = filtered.Where(t => t.statusid == Convert.ToInt32(HttpContext.Request.Query["statusid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["categoryid"]) != 0) + { + filtered = filtered.Where(t => t.categoryid == Convert.ToInt32(HttpContext.Request.Query["categoryid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["assignedtoid"]) != 0) + { + filtered = filtered.Where(t => t.assignedtoid == Convert.ToInt32(HttpContext.Request.Query["assignedtoid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["projectid"]) != 0) + { + filtered = filtered.Where(t => t.projectid == Convert.ToInt32(HttpContext.Request.Query["projectid"])); + } + if (HttpContext.Request.Query["priority"].ToString() != "") + { + ScrumPrio priority; + Enum.TryParse(HttpContext.Request.Query["priority"].ToString(), out priority); + filtered = filtered.Where(t => t.priority == priority); + } + + + return filtered.ToList(); } // GET: api/tasks/5 diff --git a/ScrumTaskboard/Controllers/UsersController.cs b/ScrumTaskboard/Controllers/UsersController.cs index ec5e47a..1194d08 100644 --- a/ScrumTaskboard/Controllers/UsersController.cs +++ b/ScrumTaskboard/Controllers/UsersController.cs @@ -21,7 +21,13 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetUser() { - return await _context.Users.ToListAsync(); + IEnumerable filtered = _context.Users; + if (HttpContext.Request.Query["name"].ToString() != "") + { + filtered = filtered.Where(u => u.name.Contains(HttpContext.Request.Query["name"].ToString())); + } + + return filtered.ToList(); } // GET: api/sprint/5 diff --git a/ScrumTaskboard/Controllers/UserstoriesController.cs b/ScrumTaskboard/Controllers/UserstoriesController.cs index 92cb885..2537074 100644 --- a/ScrumTaskboard/Controllers/UserstoriesController.cs +++ b/ScrumTaskboard/Controllers/UserstoriesController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; @@ -21,7 +22,40 @@ namespace ScrumTaskboard.Controllers [HttpGet] public async Task>> GetUserstory() { - return await _context.Userstories.ToListAsync(); + IEnumerable filtered = _context.Userstories; + if (HttpContext.Request.Query["title"].ToString() != "") + { + filtered = filtered.Where(t => t.title.Contains(HttpContext.Request.Query["title"].ToString())); + } + if (Convert.ToInt32(HttpContext.Request.Query["statusid"]) != 0) + { + filtered = filtered.Where(t => t.statusid == Convert.ToInt32(HttpContext.Request.Query["statusid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["categoryid"]) != 0) + { + filtered = filtered.Where(t => t.categoryid == Convert.ToInt32(HttpContext.Request.Query["categoryid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["createdby"]) != 0) + { + filtered = filtered.Where(t => t.createdbyid == Convert.ToInt32(HttpContext.Request.Query["createdby"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["projectid"]) != 0) + { + filtered = filtered.Where(t => t.projectid == Convert.ToInt32(HttpContext.Request.Query["projectid"])); + } + if (Convert.ToInt32(HttpContext.Request.Query["sprintid"]) != 0) + { + filtered = filtered.Where(t => t.sprintid == Convert.ToInt32(HttpContext.Request.Query["sprintid"])); + } + if (HttpContext.Request.Query["priority"].ToString() != "") + { + ScrumPrio priority; + Enum.TryParse(HttpContext.Request.Query["priority"].ToString(), out priority); + filtered = filtered.Where(t => t.priority == priority); + } + + + return filtered.ToList(); } // GET: api/userstories/1