public async Task <ActionResult> UpdatePaymentCard(PaymentCardUpdateDTO paymentCardUpdateDTO)
        {
            // Get the userId from the token
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var user = await _repo.GetUserById(userId);

            _mapper.Map(paymentCardUpdateDTO, user.PaymentCard);

            _repo.Update(user);

            // if the repo is saved successfully, return NoContent
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }


            // if the error comes out
            return(BadRequest("Failed to update payment card"));
        }
        public async Task <ActionResult> UpdateDriver(DriverUpdateDTO driverUpdateDTO)
        {
            // Get the userId from the token
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var user = await _repo.GetUserById(userId);

            // Check the current UserMode to restirct updating from the owner mode
            if (user.CurrentUserMode == UserMode.Driver)
            {
                _mapper.Map(driverUpdateDTO, user);

                _repo.Update(user);

                if (await _repo.SaveAll())
                {
                    return(NoContent());
                }
            }

            return(BadRequest("Failed to update user"));
        }
        public async Task <ActionResult> UpdateVehicle(VehicleUpdateDTO vehicleUpdateDTO)
        {
            // Get the userId from the token
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var vehicle = await _repo.GetVehicleByUserId(userId);

            if (vehicle.UserId == userId)
            {
                _mapper.Map(vehicleUpdateDTO, vehicle);

                _repo.Update(vehicle);

                // if the repo is saved successfully, return NoContent
                if (await _repo.SaveAll())
                {
                    return(NoContent());
                }
            }

            // if the error comes out
            return(BadRequest("Failed to update vehicle"));
        }
        public async Task <ActionResult> AddReport([FromBody] Report report)
        {
            if (report != null)
            {
                _repo.Add(report);
            }

            if (await _repo.SaveAll())
            {
                return(Ok("Success"));
            }

            return(BadRequest("Failed"));
        }
        public async Task <ActionResult> UpdateUserVerified(string id, UserUpdateForAdminDTO userUpdateForAdminDTO)
        {
            // Get the userId from the token
            var user = await _repo.GetUserById(id);

            if (user != null)
            {
                _mapper.Map(userUpdateForAdminDTO, user);

                _repo.Update(user);

                if (await _repo.SaveAll())
                {
                    return(NoContent());
                }
            }

            return(BadRequest("Failed to update user"));
        }
예제 #6
0
        public async Task <ActionResult> UpdateParkingLot(int parkingLotId, ParkingLotUpdateDTO parkingLotUpdateDTO)
        {
            // Get the userId from the token
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var parkinglot = await _repo.GetParkingLotById(parkingLotId);

            // If the parkinglot is listed by the current user, then the user can update
            if (parkinglot.UserId == userId)
            {
                _mapper.Map(parkingLotUpdateDTO, parkinglot);

                _repo.Update(parkinglot);

                // if the repo is saved successfully, return NoContent
                if (await _repo.SaveAll())
                {
                    return(NoContent());
                }
            }

            // if the error comes out
            return(BadRequest("Failed to update parking lot"));
        }
        public async Task <ActionResult> CreateReservation(int parkingLotId, ReservationCreateDTO reservationCreateDTO)
        {
            // Get the userId from the token
            var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            var userFromRepo = await _repo.GetUserById(userId);

            var reservation = _mapper.Map <Reservation>(reservationCreateDTO);

            var vehicle = userFromRepo.Vehicles.FirstOrDefault();

            var parkinglot = await _repo.GetParkingLotById(parkingLotId);


            if (vehicle != null && parkinglot != null)
            {
                _repo.Add(reservation);
            }
            else
            {
                BadRequest("Could not add the reservation");
            }

            // Save a new reservation
            if (await _repo.SaveAll())
            {
                // For driver
                var userReservation = new AppUserReservation
                {
                    User          = userFromRepo,
                    UserId        = userId,
                    Reservation   = reservation,
                    ReservationId = reservation.Id
                };

                _repo.Add(userReservation);

                // For owner
                var ownerReservation = new AppUserReservation
                {
                    User          = parkinglot.User,
                    UserId        = parkinglot.UserId,
                    Reservation   = reservation,
                    ReservationId = reservation.Id
                };

                _repo.Add(ownerReservation);

                var reservedParkingLot = new ReservedParkingLot
                {
                    PhotoUrl   = parkinglot.PhotoUrl,
                    Title      = parkinglot.Title,
                    City       = parkinglot.City,
                    Address    = parkinglot.Address,
                    PostalCode = parkinglot.PostalCode
                };

                //_repo.Add(reservedParkingLot);

                reservation.ReservedParkingLot = reservedParkingLot;

                if (await _repo.SaveAll())
                {
                    var reservationToReturn = _mapper.Map <ReservationDTO>(reservation);
                    return(CreatedAtRoute("GetReservation", new { id = reservation.Id }, reservationToReturn));
                }
            }

            return(BadRequest("Could not add the reservation"));
        }