public async Task <IActionResult> UpdateRental(int id, [FromBody] RentalResource rentalResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var rental = await rentalRepository.GetRental(id);

            if (rental == null)
            {
                return(NotFound());
            }
            var existingRental = await rentalRepository.FindRental(rentalResource);

            if (existingRental != null)
            {
                ModelState.AddModelError("Message", "Rental update error. Sorry, this rental record already exists!");
                return(BadRequest(ModelState));
            }
            if (rentalResource.StartDate >= rentalResource.EndDate)
            {
                ModelState.AddModelError("Message", "Rental creation error. Sorry, start date must be eailier than end date!");
                return(BadRequest(ModelState));
            }
            mapper.Map <RentalResource, Rental>(rentalResource, rental);
            rental.Property = await propertyRepository.GetProperty(rentalResource.PropertyId);

            rental.Tenant = await tenantRepository.GetTenant(rentalResource.TenantId);

            await unitOfWork.CompleteAsync();

            return(Ok(rental));
        }
Esempio n. 2
0
 public async Task <Rental> FindRental(RentalResource rentalResource)
 {
     return(await context.Rentals.SingleOrDefaultAsync(record =>
                                                       record.PropertyId == rentalResource.PropertyId &&
                                                       record.TenantId == rentalResource.TenantId &&
                                                       record.Payment == rentalResource.Payment &&
                                                       record.StartDate == rentalResource.StartDate &&
                                                       record.EndDate == rentalResource.EndDate));
 }
Esempio n. 3
0
        public async Task <IActionResult> PostRental(RentalResource resource)
        {
            var user = await _userManager.FindByIdAsync(resource.UserId);

            var rental = new Rental()
            {
                User         = user,
                DateCreated  = DateTime.Now,
                DeadlineDate = DateTime.Now.AddDays(1),
                IsFinished   = false
            };

            var booksLink = new List <BookRental>();

            foreach (var bookId in resource.BookIds)
            {
                var book = await _context.Books
                           .Include(b => b.Author)
                           .Include(b => b.RentalLinks)
                           .SingleOrDefaultAsync(b => b.Id == bookId);

                var bookRental = new BookRental()
                {
                    Book     = book,
                    BookId   = book.Id,
                    Rental   = rental,
                    RentalId = rental.Id
                };

                booksLink.Add(bookRental);

                book.RentalLinks.Add(bookRental);
            }

            rental.BooksLink = booksLink;

            user.Rentals.Add(rental);

            await _context.Rentals.AddAsync(rental);

            await _context.SaveChangesAsync();

            return(Ok(resource));
        }