public bool UpdateLending(int id, LendingModel lending) { if (lending.ReturnDate == null) { try { var dbLending = db.Lendings.FirstOrDefault(x => x.Id == id); dbLending.ReturnDate = lending.ReturnDate; var bookCopy = db.BookCopies.FirstOrDefault( x => x.BookId == lending.BookId && x.LibraryId == lending.LibraryId); bookCopy.NumberOfCopies = bookCopy.NumberOfCopies + 1; db.SaveChanges(); return(true); } catch (Exception ex) { return(false); } } return(false); }
public bool InsertLending(LendingModel lending) { try { var dbLending = new Lending() { LendingDate = lending.LendingDate, ReturnDate = lending.ReturnDate, BookId = lending.BookId, ClientId = lending.ClientId, LibraryId = lending.LibraryId }; db.Lendings.Add(dbLending); var bookCopy = db.BookCopies.FirstOrDefault( x => x.BookId == lending.BookId && x.LibraryId == lending.LibraryId); bookCopy.NumberOfCopies = bookCopy.NumberOfCopies - 1; db.SaveChanges(); return(true); } catch (Exception ex) { return(false); } }
public IHttpActionResult GetById(int id) { LendingModel result = lendingService.GetById(id); if (result != null) { return(Ok(result)); } return(NotFound()); }
public IHttpActionResult InsertLending([FromBody] LendingModel lending) { var insertLending = lendingService.InsertLending(lending); if (insertLending) { return(Ok()); } return(Content(HttpStatusCode.BadRequest, "Bad object")); }
public IHttpActionResult UpdateLending(int id, [FromBody] LendingModel lending) { LendingModel result = lendingService.GetById(id); if (result != null) { var updateLending = lendingService.UpdateLending(id, lending); if (updateLending) { return(Ok()); } } return(NotFound()); }
public LendingModel GetById(int id) { var dbLending = db.Lendings.FirstOrDefault(x => x.Id == id); if (dbLending == null) { return(null); } else { var lending = new LendingModel() { Id = dbLending.Id, LendingDate = dbLending.LendingDate, ReturnDate = dbLending.ReturnDate, BookId = dbLending.BookId, ClientId = dbLending.ClientId, LibraryId = dbLending.LibraryId }; return(lending); } }