Exemplo 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()
            });
        }
Exemplo n.º 2
0
        public async Task <QueryResults <TalkDto> > CreateTalk(Guid me, TalkDto talkDto)
        {
            var userMe = await db.Users.GetByIdAsync(me) ?? throw new NotFoundApiException();

            var receiver = await db.Users.GetByIdAsync(talkDto.Receiver) ?? throw new NotFoundApiException();

            var talk = talkDto.ConvertToModel(userMe, receiver);

            db.Talks.Add(talk);
            await db.SaveChangesAsync();

            return(new QueryResults <TalkDto>
            {
                Data = talk.ConvertToDto()
            });
        }
Exemplo n.º 3
0
        public async Task <QueryResults <UserDto> > AddUser(UserDto userDto, Guid id)
        {
            userDto.Id = id;

            var createdUser = userDto.ConvertToModel();

            createdUser.Wallet      = new Wallet();
            createdUser.Inscription = DateTime.UtcNow;

            await db.Users.AddAsync(createdUser);

            await db.SaveChangesAsync();

            return(new QueryResults <UserDto>
            {
                Data = createdUser.ConvertToDto()
            });
        }
Exemplo n.º 4
0
        public async Task <QueryResults <GardenDto> > AddGarden(Guid me, GardenDto dto)
        {
            var owner = await db.Users.GetByIdAsync(me) ?? throw new NotFoundApiException();

            dto.IsReserved = false;
            dto.Validation = Status.Accepted;
            var garden = dto.ConvertToModel();

            owner.Gardens = owner.Gardens ?? new List <Garden>();
            owner.Gardens.Add(garden);

            await db.SaveChangesAsync();

            return(new QueryResults <GardenDto>
            {
                Data = garden.ConvertToDto()
            });
        }
Exemplo n.º 5
0
        public async Task <QueryResults <PaymentDto> > AddPayment(PaymentDto dto)
        {
            var leasing = await db.Leasings.Include(l => l.Payment)
                          .Include(l => l.Garden)
                          .ThenInclude(g => g.Owner)
                          .ThenInclude(u => u.Wallet)
                          .Include(l => l.Garden)
                          .ThenInclude(g => g.Criteria)
                          .Include(l => l.Renter)
                          .ThenInclude(u => u.Wallet)
                          .GetByIdAsync(dto.Leasing) ?? throw new NotFoundApiException();

            if (!leasing.State.Equals(LeasingStatus.InDemand))
            {
                throw new BadRequestApiException("It is no longer possible to pay for this contract.");
            }

            if (leasing.Garden.Criteria.Price * leasing.MonthDifference != dto.Sum)
            {
                throw new BadRequestApiException("You are not paying the right amount.");
            }

            if (leasing.Payment != null)
            {
                throw new BadRequestApiException("You have already paid for this garden.");
            }

            dto.Date        = DateTime.UtcNow.ToTimestamp();
            leasing.Payment = dto.ConvertToModel();

            leasing.State = LeasingStatus.InProgress;
            leasing.Renter.Wallet.TrueBalance       -= dto.Sum;
            leasing.Garden.Owner.Wallet.TrueBalance += dto.Sum;

            await db.SaveChangesAsync();

            return(new QueryResults <PaymentDto>
            {
                Data = leasing.Payment.ConvertToDto()
            });
        }
Exemplo n.º 6
0
        public async Task <QueryResults <ContactDto> > Contact(Guid me, Guid contactId, DemandDto demand)
        {
            _ = await db.Users.GetByIdAsync(me) ?? throw new NotFoundApiException();

            var contact = await db.Users.GetByIdAsync(contactId) ?? throw new NotFoundApiException();

            var newContact = new Contact
            {
                Me           = me,
                MyContact    = contact,
                FirstMessage = demand.FirstMessage,
                Status       = Status.Pending
            };

            db.Contacts.Add(newContact);
            await db.SaveChangesAsync();

            return(new QueryResults <ContactDto>
            {
                Data = newContact.ConvertToDto()
            });
        }
Exemplo n.º 7
0
        public async Task <QueryResults <ScoreDto> > AddScore(Guid myId, Guid ratedId, ScoreDto dto)
        {
            if (!db.Users.Any(u => u.Id.Equals(ratedId)) && !db.Gardens.Any(g => g.Id.Equals(ratedId)))
            {
                throw new NotFoundApiException();
            }

            var me = await db.Users.GetByIdAsync(myId) ?? throw new NotFoundApiException();

            var score = dto.ConvertToModel();

            score.Rated = ratedId;

            me.AsRater = me.AsRater ?? new List <Score>();
            me.AsRater.Add(score);

            await db.SaveChangesAsync();

            return(new QueryResults <ScoreDto>
            {
                Data = score.ConvertToDto()
            });
        }
Exemplo n.º 8
0
        public async Task <QueryResults <WalletDto> > ChangeWallet(Guid me, Guid walletId, WalletDto walletDto, bool isAdmin)
        {
            var wallet = await db.Wallets.Include(w => w.OfUser)
                         .ThenInclude(u => u.AsRenter)
                         .ThenInclude(l => l.Garden)
                         .ThenInclude(g => g.Criteria)
                         .GetByIdAsync(walletId) ?? throw new NotFoundApiException();

            if (!wallet.OfUser.Id.Equals(me) && !isAdmin)
            {
                throw new ForbiddenApiException();
            }

            wallet.TrueBalance += walletDto.Balance;
            await db.SaveChangesAsync();

            return(new QueryResults <WalletDto>
            {
                Data = wallet.ConvertToDto()
            });
        }