From f842a56ce0c855916a240a82516683baa84f7013 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Sat, 19 Jun 2021 14:29:54 +0200 Subject: [PATCH] Add update order method --- .../Controllers/SectionController.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs index bf8fa76..21daf5b 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -462,6 +462,52 @@ namespace ManagerService.Controllers } } + /// + /// Update sections order + /// + /// New sections order + [ProducesResponseType(typeof(string), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut("order")] + public ObjectResult UpdateOrder([FromBody] List updatedSectionsOrder) + { + try + { + if (updatedSectionsOrder == null) + throw new ArgumentNullException("Sections param is null"); + + foreach (var section in updatedSectionsOrder) + { + if (!_sectionService.IsExist(section.Id)) + throw new KeyNotFoundException($"Section {section.Label} with id {section.Id} does not exist"); + } + + foreach (var updatedSection in updatedSectionsOrder) + { + Section section = _sectionService.GetById(updatedSection.Id); + section.Order = updatedSection.Order; + + _sectionService.Update(section.Id, section); + } + + return new ObjectResult("Sections order has been successfully modified") { StatusCode = 200 }; + } + catch (ArgumentNullException ex) + { + return new BadRequestObjectResult(ex.Message) { }; + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + /// /// Delete a section