public async Task <IActionResult> AddMovieToCollection([FromBody] AddMovieToCollectionInput model) { try { var result = await collectionLib.AddMovieToCollectionAsync(model, CurrentUserId); return(CustomResult(result)); } catch (System.Exception exp) { return(CustomError(exp)); } }
public async Task <bool> AddMovieToCollectionAsync(AddMovieToCollectionInput model, long currentUserId) { var collectionEntity = await collectionMovieRepo.FirstAsync(x => x.Id == model.CollectionId && x.UserId == currentUserId); if (collectionEntity == null) { throw new System.Exception("Collection not exist"); } var movieEntity = await movieRepo.FirstAsync(x => x.Id == model.MovieId && x.UserId == currentUserId); if (movieEntity == null) { throw new System.Exception("Movie not exist"); } if (collectionEntity.MovieCollectionDetails.Any(x => x.MovieId == model.MovieId)) { throw new System.Exception("Movie is exist in collection"); } collectionEntity.MovieCollectionDetails.Add(new MovieCollectionDetail { MovieId = model.MovieId, Number = model.Number }); collectionMovieRepo.Update(collectionEntity); await unitOfWork.CommitAsync(); return(true); }