public async Task <ActionResult <Borrow> > PostBorrow(AddBorrowDto borrow) { var user = User.Claims.FirstOrDefault(c => c.Type == "Id")?.Value; if (user == null) { return(Unauthorized()); } var type = (TypeEnum)TypeEnum.Parse(typeof(TypeEnum), User.Claims.FirstOrDefault(c => c.Type == "Type").Value); var rule = await _context.Rule.FirstOrDefaultAsync(r => r.Type == type); var amount = rule.BookAmount; var count = _context.Borrow.Count(b => b.User.PID == user && (b.Status == TypeEnum.Borrowing || b.Status == TypeEnum.Wait)); if (amount > count) { var newUser = new User() { PID = user }; _context.Attach(newUser); var book = new Book() { BookID = borrow.Book }; _context.Attach(book); var newBorrow = new Borrow() { User = newUser, Book = book, Status = TypeEnum.Wait, }; _context.Borrow.Add(newBorrow); await _context.SaveChangesAsync(); return(CreatedAtAction("GetBorrow", new { id = newBorrow.BorrowId }, borrow)); } return(BadRequest("Book Limited")); }
public async Task <ActionResult <Borrow> > PutBorrow(int id, UpdateBorrowDto update) { var type = (TypeEnum)TypeEnum.Parse(typeof(TypeEnum), User.Claims.FirstOrDefault(c => c.Type == "Type").Value); if (type != TypeEnum.Admin) { return(Unauthorized()); } var borrow = await _context.Borrow.Include(b => b.Book).FirstOrDefaultAsync(b => b.BorrowId == id); var rule = await _context.Rule.FirstOrDefaultAsync(r => r.Type == type); var duedate = borrow.BorrowDate.AddDays(rule.LimitDay); var book = await _context.Book.FirstOrDefaultAsync(b => b.BookID == borrow.Book.BookID); if (update.Status == TypeEnum.Losted && duedate < DateTime.Now) { borrow.Fines = book.Price * 2 + (5 * (int)((DateTime.Now - duedate).TotalDays)); } else if (update.Status == TypeEnum.Losted) { borrow.Fines = book.Price * 2; } else if (update.Status == TypeEnum.Late) { borrow.Fines = 5 * (int)((DateTime.Now - duedate).TotalDays); } borrow.Status = update.Status; await _context.SaveChangesAsync(); return(NoContent()); }