Ejemplo n.º 1
0
        public async Task <QueryResults <LeasingDto> > AddLeasing(Guid me, LeasingDto dto)
        {
            var renter = await db.Users.Include(u => u.Wallet)
                         .ThenInclude(w => w.OfUser)
                         .ThenInclude(u => u.AsRenter)
                         .ThenInclude(l => l.Garden)
                         .ThenInclude(g => g.Criteria)
                         .GetByIdAsync(me) ?? throw new NotFoundApiException();

            var garden = await db.Gardens.Include(g => g.Owner)
                         .Include(g => g.Criteria)
                         .GetByIdAsync(dto.Garden) ?? throw new NotFoundApiException();

            if (!garden.Validation.Equals(Status.Accepted))
            {
                throw new BadRequestApiException("The garden must be validated to be able to rent it.");
            }

            if (garden.Owner.Id.Equals(me))
            {
                throw new BadRequestApiException("You are not authorize to rent your own garden.");
            }

            if (dto.End <= dto.Begin)
            {
                throw new BadRequestApiException("Your end date is less than your start date.");
            }

            dto.Creation = DateTime.UtcNow.ToTimestamp();
            dto.State    = LeasingStatus.InDemand;
            dto.Renew    = false;
            dto.Renter   = renter.Id;

            var leasing = dto.ConvertToModel();

            if (renter.Wallet.RealTimeBalance - (garden.Criteria.Price * leasing.MonthDifference) < 0)
            {
                throw new BadRequestApiException("You have not enough money to rent this garden.");
            }

            renter.AsRenter = renter.AsRenter ?? new List <Leasing>();
            renter.AsRenter.Add(leasing);

            garden.Leasings = garden.Leasings ?? new List <Leasing>();
            garden.Leasings.Add(leasing);

            await db.SaveChangesAsync();

            return(new QueryResults <LeasingDto>
            {
                Data = leasing.ConvertToDto()
            });
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateLeasing([FromBody] LeasingDto leasingDto)
        {
            try
            {
                var me     = ExtractIdFromToken(Request.Headers[HttpRequestHeader.Authorization.ToString()]);
                var result = await service.AddLeasing(me, leasingDto);

                return(Created($"/api/Leasing/{result.Data.Id}", result));
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception)
            {
                throw new BadRequestApiException();
            }
        }
Ejemplo n.º 3
0
 public async Task <IActionResult> ChangeLeasing([FromRoute(Name = "id")] Guid leasingId, [FromBody] LeasingDto leasingDto)
 {
     try
     {
         var me = ExtractIdFromToken(Request.Headers[HttpRequestHeader.Authorization.ToString()]);
         return(Ok(await service.ChangeLeasing(me, leasingId, leasingDto, IsAdmin(Request.Headers[HttpRequestHeader.Authorization.ToString()]))));
     }
     catch (HttpResponseException)
     {
         throw;
     }
     catch (Exception)
     {
         throw new BadRequestApiException();
     }
 }
Ejemplo n.º 4
0
        public async Task <QueryResults <LeasingDto> > ChangeLeasing(Guid me, Guid leasingId, LeasingDto dto, bool isAdmin)
        {
            var leasing = db.Leasings.Include(l => l.Garden)
                          .Include(l => l.Renter)
                          .Include(l => l.Garden.Owner)
                          .GetByIdAsync(leasingId).Result ?? throw new NotFoundApiException();

            if (!leasing.Renter.Id.Equals(me) && !leasing.Garden.Owner.Id.Equals(me) && !isAdmin)
            {
                throw new ForbiddenApiException();
            }

            leasing.Begin = dto.Begin?.ToDateTime() ?? leasing.Begin;
            leasing.End   = dto.End?.ToDateTime() ?? leasing.End;
            leasing.Renew = dto.Renew ?? leasing.Renew;
            leasing.State = dto.State ?? leasing.State;

            db.Update(leasing);
            await db.SaveChangesAsync();

            return(new QueryResults <LeasingDto>
            {
                Data = leasing.ConvertToDto()
            });
        }