//public async Task Transact(IGenericRepository<User> users, IGenericRepository<Movie> movies, // User user, Movie movie) //{ // var transaction = CreateTransaction(user, movie); // using var transact = _context.Database.BeginTransaction(); // try // { // await movies.Add(movie); // users.Update(user); // await Add(transaction); // await _context.SaveChangesAsync(); // transact.Commit(); // } // catch (Exception e) // { // Debug.WriteLine(e.Message); // throw; // } //} public async Task Transact(IGenericRepository <User> users, IGenericRepository <Movie> movies, IRatingsRepository ratings, User user, Movie movie, Rating rating) { var transaction = CreateTransaction(user, movie); using var transact = _context.Database.BeginTransaction(); try { await ratings.Add(rating); users.Update(user); await Add(transaction); await _context.SaveChangesAsync(); transact.Commit(); } catch (Exception e) { Debug.WriteLine(e.Message); throw; } }
public async Task <IActionResult> Post([FromBody] RatingInput rating) { try { var validationResult = rating.Validate(); if (!validationResult.IsValid) { return(BadRequest(validationResult.Message)); } var created = await _ratingsRepository.Add(rating); if (created == null) { return(BadRequest($"There is already a rating with the name {rating.Name}")); } return(Created($"api/ratings/{created.Id}", created)); } catch (Exception) { return(new StatusCodeResult(500)); } }