public ActionResult Create(RatedDto dto) { if (!ModelState.IsValid) { TempData["error"] = "Check your input."; return(RedirectToAction(nameof(Create))); } try { executor.ExecuteCommand(addRated, dto); return(RedirectToAction(nameof(Index))); } catch (EntityNotAllowedException) { return(RedirectToAction("PageNotFound", "Redirections")); } catch (EntityAlreadyExistsException e) { TempData["error"] = e.Message; } catch (Exception e) { TempData["error"] = e.Message; } return(RedirectToAction(nameof(Index))); }
public IActionResult Post([FromBody] RatedDto dto) { try { addRated.Execute(dto); return(StatusCode(201)); } catch (EntityAlreadyExistsException e) { return(StatusCode(409, new { Errors = new List <string> { e.Message } })); } catch (Exception e) { return(StatusCode(500, new { Errors = new List <string> { e.Message } })); } }
public void Execute(RatedDto request) { if (Context.Rateds.Any(r => r.Name.ToLower() == request.Name.ToLower())) { throw new EntityAlreadyExistsException("Rating"); } Context.Rateds.Add(new Rated { Name = request.Name, }); Context.SaveChanges(); }
public void Execute(RatedDto dto) { var rating = Context.Rateds.Find(dto.Id); if (rating == null || rating.IsDeleted == true) { throw new EntityNotFoundException("Rating"); } if (dto.Name.ToLower() != rating.Name.ToLower() && Context.Rateds.Any(r => r.Name.ToLower() == dto.Name.ToLower())) { throw new EntityAlreadyExistsException("Rating"); } rating.Name = dto.Name; Context.SaveChanges(); }
public ActionResult Edit(int id, RatedDto dto) { try { dto.Id = id; executor.ExecuteCommand(editRated, dto); return(RedirectToAction(nameof(Index))); } catch (EntityNotAllowedException) { return(RedirectToAction("PageNotFound", "Redirections")); } catch (EntityAlreadyExistsException e) { TempData["error"] = e.Message; } catch (Exception e) { TempData["error"] = e.Message; } return(RedirectToAction(nameof(Index))); }
public IActionResult Put(int id, [FromBody] RatedDto dto) { try { dto.Id = id; editRated.Execute(dto); return(StatusCode(204)); } catch (EntityAlreadyExistsException e) { return(StatusCode(409, new { Errors = new List <string> { e.Message } })); } catch (EntityNotFoundException e) { return(NotFound(new { Errors = new List <string> { e.Message } })); } catch (Exception e) { return(StatusCode(500, new { Errors = new List <string> { e.Message } })); } }