public ActionResult Create([FromForm] PosterDto dto) { if (!ModelState.IsValid) { TempData["error"] = "Something went wrong, check input and try again."; return(RedirectToAction(nameof(Create))); } try { addPoster.Execute(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 void Execute(PosterDto request) { var ext = Path.GetExtension(request.Image.FileName); var size = request.Image.Length; if (size / 1000 > 4096) { throw new EntityNotAllowedException("File size bigger than 4MB"); } if (!FileUpload.AllowedExtensions.Contains(ext)) { throw new EntityNotAllowedException("Extension " + ext); } var newFileName = Guid.NewGuid().ToString() + "_" + request.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); request.Image.CopyTo(new FileStream(filePath, FileMode.Create)); Context.Posters.Add(new Poster { PosterTitle = request.PosterTitle, Alt = request.Alt, Name = newFileName, MovieId = request.MovieId, }); Context.SaveChanges(); }
public IActionResult Post([FromForm] PosterDto dto) { try { addPoster.Execute(dto); return(StatusCode(201)); } catch (Exception e) { return(StatusCode(500, new { Errors = new List <string> { e.Message } })); } }
public void Execute(PosterDto request) { var poster = Context.Posters.Find(request.Id); if (poster == null || poster.IsDeleted == true) { throw new EntityNotFoundException("Poster"); } if (poster.Name.ToLower() == request.Name.ToLower()) { throw new EntityAlreadyExistsException("Poster name"); } poster.PosterTitle = request.PosterTitle; poster.Alt = request.Alt; poster.Name = request.Name; poster.MovieId = request.MovieId; Context.SaveChanges(); }
public ActionResult Edit(int id, [FromBody] PosterDto dto) { try { dto.Id = id; editPoster.Execute(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] PosterDto dto) { try { dto.Id = id; editPoster.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 } })); } }