コード例 #1
0
        public async Task <IActionResult> AddTripToClient([FromRoute] int idTrip, [FromBody] AddTripToClientRequestDto dto)
        {
            try
            {
                await repository.AddTripToClientAsync(idTrip, dto);

                return(Ok("Your request processed successfully!"));
            }
            catch (Exception e)
            {
                return(NotFound(e.Message));
            }
        }
コード例 #2
0
ファイル: TripDbRepository.cs プロジェクト: ceswer/APBD
        public async Task AddTripToClientAsync(int idTrip, AddTripToClientRequestDto dto)
        {
            bool ClientExists = await contex.Clients.AnyAsync(row => row.Pesel == dto.Pesel);

            Client wantedClient;

            if (!ClientExists)
            {
                wantedClient = new Client
                {
                    IdClient  = await contex.Clients.Select(row => row.IdClient).MaxAsync() + 1,
                    FirstName = dto.FirstName,
                    LastName  = dto.LastName,
                    Email     = dto.Email,
                    Telephone = dto.Telephone,
                    Pesel     = dto.Pesel
                };

                await contex.Clients.AddAsync(wantedClient);

                await contex.SaveChangesAsync();
            }
            else
            {
                wantedClient = await contex.Clients.FirstOrDefaultAsync(row => row.Pesel == dto.Pesel);
            }


            /*
             *  Note: it seems like point 3(c) should be earlier than 2(c).
             *
             *  But really idk if it makes any sense.
             */

            bool TripExists = await contex.Trips.AnyAsync(row => row.IdTrip == idTrip);

            if (!TripExists)
            {
                throw new Exception($"There is no such trip with ID: {idTrip}!");
            }


            /*
             *  ii. Czy klient nie jest już zapisany na wspomnianą wycieczkę – w takim wypadku zwracamy błąd.
             *
             *  Note: the main idea is to add client to the trip,
             *  so it`s never true when we want to asign a client to new trip.
             *
             *  Or I didn`t understand that correctly.
             */

            bool isClientAlreadyReservedThisTrip = await contex.ClientTrips.AnyAsync(row => row.IdClient == wantedClient.IdClient && row.IdTrip == idTrip);

            if (isClientAlreadyReservedThisTrip)
            {
                throw new Exception("Client is already reserved that trip!");
            }


            await contex.ClientTrips.AddAsync(new ClientTrip
            {
                IdClient     = wantedClient.IdClient,
                IdTrip       = idTrip,
                RegisteredAt = DateTime.Now,
                PaymentDate  = dto.PaymentDate
            });

            await contex.SaveChangesAsync();
        }