public async Task <ActionResult> UnReserve(int id, [FromHeader] CancellationToken cancellationToken) { var userId = User.Identity.GetUserId(); var user = await _userManager.FindByIdAsync(userId); ViewBag.FullName = user.FullName; Book book = await _bookRepository.TableNoTracking .Where(c => c.Id == id && c.BookIsDeleted == false) .SingleOrDefaultAsync(cancellationToken); if (book == null) { return(new JsonResult("کتاب مورد نظر یافت نشد")); } ReserveBook rb = await _rbRepository.Table.Where(c => c.UserId == userId && c.BookId == id).SingleOrDefaultAsync(cancellationToken); string message = "این کتاب را تا به حال رزرو نکرده اید"; if (rb == null) { return(new JsonResult(message)); } await _rbRepository.DeleteAsync(rb, cancellationToken); message = "با موفقیت حذف شد"; return(new JsonResult(message)); }
private void PrepareMail(List <SystemOption> SystemData, ReserveBook reserveBook) { try { string body = string.Empty; body += "<h5> Hi " + reserveBook.Member.FirstName + " " + reserveBook.Member.LastName + "</h5>"; body += "<p>Your Reservation Id - <b style = \"color: red\">RSV" + reserveBook.Id + "</b> is Expired</p>"; body += " </br><p>Thanks & Best Regards,</p></br><p>LMS Admin.</p>"; EmailMessage message = new EmailMessage(); message.Subject = "Reservation Expired"; message.To = reserveBook.Member.Email; message.MessageBody = body; OutgoingMailSetting outgoingMailSetting = new OutgoingMailSetting(); outgoingMailSetting.DisplayName = "kuganrajh"; outgoingMailSetting.MailAddress = SystemData.Where(s => s.Name == MailDetail.FromEmailId.ToString()).FirstOrDefault().Value; outgoingMailSetting.Password = SystemData.Where(s => s.Name == MailDetail.FromEmailPassword.ToString()).FirstOrDefault().Value; outgoingMailSetting.SmtpServer = SystemData.Where(s => s.Name == MailDetail.SmtpClient.ToString()).FirstOrDefault().Value; outgoingMailSetting.SmtpPort = Convert.ToInt32(SystemData.Where(s => s.Name == MailDetail.PortId.ToString()).FirstOrDefault().Value); EmailManager.SendEmail(message, outgoingMailSetting); } catch (Exception ex) { LogManager.Log(LogSeverity.Error, LogModule.Web, ex.Message, ex); } }
public async Task <HttpResponseMessage> Put(int id, ReserveBook model) { if (!ModelState.IsValid || id != model.Id) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } model.UpdatedAt = DateTime.Now; applicationResult.Result = await _service.UpdateAsync(model); return(Request.CreateResponse <ApplicationResult>(HttpStatusCode.OK, applicationResult)); }
public async Task <IActionResult> Borrow(string userId, int bookid, [FromHeader] CancellationToken cancellationToken) { var setting = await _sRepository.GetByIdAsync(cancellationToken, 1); var borrowCount = await _rbRepository.TableNoTracking .Where(c => c.BookStatus == BookStatus.Borrowed && c.UserId == userId).CountAsync(cancellationToken); var rb = await _rbRepository.Table .Where(c => c.UserId == userId && c.BookId == bookid && c.BookStatus == BookStatus.Borrowed).SingleOrDefaultAsync(cancellationToken); var rbreserve = await _rbRepository.Table .Where(c => c.UserId == userId && c.BookId == bookid && c.BookStatus == BookStatus.Reserved).SingleOrDefaultAsync(cancellationToken); if (rb != null) { TempData["Error"] = "این کتاب قبلا توسط این کاربر امانت گرفته شده است"; return(RedirectToAction("Index", "Home")); } if (rb == null) { if (borrowCount >= setting.BorrowCount) { TempData["Error"] = $"کاربر بیشتر از {setting.BorrowCount} عدد کتاب نمی تواند امانت بگیرد"; return(RedirectToAction("Index", "Home")); } if (rbreserve == null) { rbreserve = new ReserveBook { BookId = bookid, BookStatus = BookStatus.Borrowed, BorrowDate = DateTime.Now, UserId = userId }; await _rbRepository.AddAsync(rbreserve, cancellationToken); } else { rbreserve.BookStatus = BookStatus.Borrowed; rbreserve.BorrowDate = DateTime.Now; await _rbRepository.UpdateAsync(rbreserve, cancellationToken); } } TempData["Success"] = "عملیات با موفقیت انجام شد"; return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> Reserve(int id, [FromHeader] CancellationToken cancellationToken) { var setting = await _settingRepo.GetByIdAsync(cancellationToken, 1); var userId = User.Identity.GetUserId(); var user = await _userRepo.TableNoTracking.Include(c => c.ReserveBooks).Where(c => c.Id == userId).SingleOrDefaultAsync(cancellationToken); ViewBag.FullName = user.FullName; Book book = await _bookRepository.TableNoTracking .Where(c => c.Id == id && c.BookIsDeleted == false).SingleOrDefaultAsync(cancellationToken); if (book == null) { return(new JsonResult("کتاب مورد نظر یافت نشد")); } ReserveBook rb = await _rbRepository.TableNoTracking.Where(c => c.UserId == userId && c.BookId == id).SingleOrDefaultAsync(cancellationToken); string message = "قبلا این کتاب را رزرو کرده اید"; if (rb != null) { return(new JsonResult(message)); } if (user.ReserveBooks.Where(c => c.BookStatus == BookStatus.Reserved).Count() >= setting.ReservCount) { return(new JsonResult($"شما نمی توانید بیشتر از {setting.ReservCount} کتاب رزرو کنید")); } ReserveBook newrb = new ReserveBook { BookId = id, BookStatus = Common.Enums.BookStatus.Reserved, ReserveDate = DateTime.Now, UserId = userId }; await _rbRepository.AddAsync(newrb, cancellationToken); message = "با موفقیت رزرو شد"; return(new JsonResult(message)); }
public async Task <ReserveBook> CheckIfBookExistInReservations(string bookId) { var book = await _context.Books.FindAsync(bookId).ConfigureAwait(false); ReserveBook reservation = null; if (await CheckIfBookWithThisTitleExist(book.Title).ConfigureAwait(false)) { var bookInReservations = _context.ReservedBooks.Where(b => b.BookTitle == book.Title); var dateOfFirstReservation = await bookInReservations.MinAsync(d => d.ReservationDate).ConfigureAwait(false); reservation = await _context.ReservedBooks.FirstAsync(d => d.ReservationDate == dateOfFirstReservation).ConfigureAwait(false); await GiveBookToFirstReservation(reservation.BookId, reservation.UserId).ConfigureAwait(false); _context.ReservedBooks.Remove(reservation); await _context.SaveChangesAsync().ConfigureAwait(false); } return(reservation); }
public async Task <bool> Reserve(string userId, int bookId, CancellationToken cancellationToken) { var setting = await _seRepository.GetByIdAsync(cancellationToken, 1); var book = await _bRepository.Table.Where(c => c.Id == bookId).SingleOrDefaultAsync(cancellationToken); if (book == null) { throw new BadRequestException("کتاب یافت نشد"); } var user = await _uRepository.Table.Where(c => c.Id == userId).SingleOrDefaultAsync(cancellationToken); if (user == null) { throw new BadRequestException("کاربر یافت نشد"); } if (user.ReserveBooks.Count >= setting.ReservCount) { return(false); } var reserveBook = new ReserveBook { BookId = bookId, UserId = userId, BookStatus = Common.Enums.BookStatus.Reserved, ReserveDate = DateTime.Now }; await _sbRepository.AddAsync(reserveBook, cancellationToken); return(true); }
public async Task <ReserveBook> ReserveBookAsync(string bookId, string userId) { var book = await _context.Books.FindAsync(bookId).ConfigureAwait(false); var reservation = await _context.ReservedBooks.FirstOrDefaultAsync(r => r.BookId == bookId && r.UserId == userId).ConfigureAwait(false); if (reservation != null) { return(null); } else { reservation = new ReserveBook { BookId = bookId, UserId = userId, BookTitle = book.Title, ReservationDate = DateTime.Now, }; await AddReservationToDb(reservation).ConfigureAwait(false); } return(reservation); }
private async Task AddReservationToDb(ReserveBook reservation) { _context.ReservedBooks.Add(reservation); //addAsync..nn }