// PUT api/Autors/5 public async Task <IHttpActionResult> PutAutor(int id, Autor autor) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != autor.id) { return(BadRequest()); } db.Entry(autor).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AutorExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task AddAsync(Book newBook) { var entity = Mapper.Map <Entities.Book>(newBook); await _context.AddAsync(entity); try { await _context.SaveChangesAsync(); } catch (DbUpdateException e) { if (e.InnerException is SqlException sqlException) { if (sqlException.Number == 547) { throw new MemberRelationException("Check entered value. ", sqlException.Message); } else { throw; } } else { throw; } } }
public async Task AddAsync(Author newAuthor) { var entity = Mapper.Map <Entities.Author>(newAuthor); await _context.AddAsync(entity); await _context.SaveChangesAsync(); }
public async Task AddAsync(User newUser) { var entity = Mapper.Map <Entities.User>(newUser); await _context.AddAsync(entity); await _context.SaveChangesAsync(); }
public async Task AddRangeAsync(IEnumerable <UserBook> userBooks) { var bookIds = userBooks.Select(u => u.BookId); var books = await _context.Book.Where(b => bookIds.Contains(b.Id)).ToListAsync(); foreach (var item in userBooks) { var book = books.Find(b => b.Id == item.BookId); if (book.AvailableCount >= item.Count) { var entity = Mapper.Map <Entities.UserBook>(item); await _context.AddAsync(entity); book.AvailableCount -= item.Count; } else { _context.Dispose(); throw new OrderedBookAvailabilityException("Books available count has been updated!"); } } await _context.SaveChangesAsync(); }
public async Task AddAsync(User user) { await _context.AddAsync(user); await _context.SaveChangesAsync(); }