public ActionResult <Film> Post(Film newfilm) { var film = _mapper.Map <Film>(newfilm); _filmRepository.AddFilm(film); return(Ok()); }
public async Task <IActionResult> Create([Bind("FilmID,Title,Year,Length")] Film film) { if (ModelState.IsValid) { _filmRepository.AddFilm(film); await _filmRepository.Save(); return(RedirectToAction(nameof(Index))); } return(View(film)); }
public async Task <IActionResult> Post([FromBody] FilmViewModel theFilm) { try { if (ModelState.IsValid) { var newFilm = Mapper.Map <Film>(theFilm); var result = _parserService.ParseLink(newFilm.Link); if (result.Success) { if (string.IsNullOrWhiteSpace(User.Identity.Name)) { newFilm.UserName = User.FindFirst(ClaimTypes.NameIdentifier).Value; } else { newFilm.UserName = User.Identity.Name; } newFilm.Title = result.Title; newFilm.OriginalTitle = result.OriginalTitle; newFilm.Cover = result.Cover; newFilm.Director = result.Director; newFilm.Screenplay = result.Screenplay; newFilm.Genre = result.Genre; newFilm.Country = result.Country; newFilm.ReleaseDate = result.ReleaseDate; newFilm.Cast = result.Cast; newFilm.Description = result.Description; string postResult = _repository.AddFilm(newFilm); if (postResult == string.Empty) { if (await _repository.SaveChangesAsync()) { string encodedUrl = WebUtility.UrlEncode($"api/films/{newFilm.Title}"); return(Created(encodedUrl, Mapper.Map <FilmViewModel>(newFilm))); } } else { ModelState.AddModelError("", postResult); } } } } catch (Exception ex) { return(BadRequest(ex.Message)); } return(BadRequest(ModelState)); }