/// <summary>
        /// Cancel a rental by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Response Cancel(int id)
        {
            var response = new Response();

            logger.LogInformation($"Calling rental repository to find rental with id {id}");

            var entity = repository.RentalRepository.Find(id);

            if (entity == null)
            {
                response.AddError(Constants.RENTAL_NOT_FOUND, "Rental not found");
                return(response);
            }

            try
            {
                entity.Status = RentalStatus.Cancelled;

                logger.LogInformation("Calling rental repository to cancel rental with id {id}");

                repository.RentalRepository.Update(entity);
                repository.Save();

                response.AddSuccess(Constants.RENTAL_CANCELLED, "The rental car was succesfully cancelled");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }
        /// <summary>
        /// Client validations
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private void ValidateClient(CreateRentalRequestModel request, Response response)
        {
            if (!request.ClientId.HasValue)
            {
                response.AddError(Constants.CLIENTID_EMPTY, "The field ClientId is required");
            }
            else
            {
                var client = repository.ClientRepository.Find(request.ClientId.Value);

                if (client == null || !client.Active)
                {
                    response.AddError(Constants.CLIENT_NOT_FOUND, "Client not found");
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Logical delete of a vehicle by id
        /// </summary>
        /// <param name="id"></param>
        public Response Delete(int id)
        {
            var response = new Response();

            logger.LogInformation($"Calling vehicle repository to find vehicle with id {id}");

            var entity = repository.VehicleRepository.Find(id);

            if (entity == null)
            {
                response.AddError(Constants.VEHICLE_NOT_FOUND, "Vehicle not found");
                return(response);
            }

            try
            {
                repository.VehicleRepository.Delete(entity);
                repository.Save();

                response.AddSuccess(Constants.VEHICLE_DELETED, "Vehicle removed succesfully");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }
        /// <summary>
        /// Logical delete of a client by id
        /// </summary>
        /// <param name="id"></param>
        public Response Delete(int id)
        {
            var response = new Response();

            logger.LogInformation($"Calling client repository to find client with id {id}");

            var entity = repository.ClientRepository.Find(id);

            if (entity == null)
            {
                response.AddError(Constants.CLIENT_NOT_FOUND, "Client not found");
                return(response);
            }

            try
            {
                logger.LogInformation("Calling client repository to delete client");

                repository.ClientRepository.Delete(entity);
                repository.Save();

                response.AddSuccess(Constants.CLIENT_DELETED, "Client removed succesfully");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }
        /// <summary>
        /// Create new rental
        /// </summary>
        /// <param name="request"></param>
        public Response Add(CreateRentalRequestModel request)
        {
            var response = new Response();

            try
            {
                logger.LogInformation("Starting request validation");

                DateUtils.ValidateRangeDates(response, request.StartDate, request.EndDate);

                var vehicle = ValidateVehicle(request, response);

                ValidateClient(request, response);

                if (response.HasErrors())
                {
                    return(response);
                }

                logger.LogInformation("Request validated success");

                logger.LogInformation("Calling rental repository to find availables vehicles by range dates");

                var isVehicleAvailable = repository.RentalRepository.VerifyIfVehicleIsAvailableByRangeDates(request.VehicleId.Value, request.StartDate.Value, request.EndDate.Value);

                if (!isVehicleAvailable)
                {
                    response.AddError(Constants.VEHICLE_NOT_AVAILABLE, "Vehicle is not available");
                    return(response);
                }

                var totalDays = request.EndDate.Value.Subtract(request.StartDate.Value).TotalDays + 1;

                var rental = new Rental
                {
                    ClientId  = request.ClientId.Value,
                    VehicleId = request.VehicleId.Value,
                    StartDate = request.StartDate.Value,
                    EndDate   = request.EndDate.Value,
                    Price     = vehicle.PricePerDay * totalDays,
                    Status    = RentalStatus.Reserved
                };

                logger.LogInformation("Calling rental repository to save new rental");

                repository.RentalRepository.Add(rental);
                repository.Save();

                response.AddSuccess(Constants.RENTAL_SAVED, "A vehicle was reserved succesfully");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }
        /// <summary>
        /// Vehicle validations
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        private Vehicle ValidateVehicle(CreateRentalRequestModel request, Response response)
        {
            if (!request.VehicleId.HasValue)
            {
                response.AddError(Constants.VEHICLEID_EMPTY, "The field VehicleId is required");
                return(null);
            }
            else
            {
                var vehicle = repository.VehicleRepository.Find(request.VehicleId.Value);

                if (vehicle == null || !vehicle.Active)
                {
                    response.AddError(Constants.VEHICLE_NOT_FOUND, "Vehicle not found");
                }

                return(vehicle);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create new vehicle
        /// </summary>
        /// <param name="request"></param>
        public Response Add(VehicleRequestModel request)
        {
            var response = new Response();

            logger.LogInformation("Starting request validation");

            if (string.IsNullOrWhiteSpace(request.Brand))
            {
                response.AddError(Constants.BRAND_EMPTY, "The field brand is required");
            }
            if (!request.PricePerDay.HasValue || request.PricePerDay <= 0)
            {
                response.AddError(Constants.PRICE_PER_DAY_INVALID, "The field pricePerDay is required");
            }

            if (response.HasErrors())
            {
                return(response);
            }

            logger.LogInformation("Request validated success");

            try
            {
                var domain = new Vehicle {
                    Brand = request.Brand, Year = request.Year, PricePerDay = request.PricePerDay.Value, Active = true
                };

                logger.LogInformation("Calling vehicle repository to save new vehicle");

                repository.VehicleRepository.Add(domain);
                repository.Save();

                response.AddSuccess(Constants.VEHICLE_SAVED, "Vehicle added succesfully");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }
        /// <summary>
        /// Create a new client
        /// </summary>
        /// <param name="request"></param>
        public Response Add(ClientRequestModel request)
        {
            var response = new Response();

            logger.LogInformation("Starting request validation");

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                response.AddError(Constants.NAME_EMPTY, "The field name is required");
            }
            if (string.IsNullOrWhiteSpace(request.Phone))
            {
                response.AddError(Constants.PHONE_EMPTY, "The field phone is required");
            }

            if (response.HasErrors())
            {
                return(response);
            }

            logger.LogInformation("Request validated success");

            try
            {
                var domain = new Client {
                    Name = request.Name, Phone = request.Phone, Active = true
                };

                logger.LogInformation("Calling client repository to save new client");

                repository.ClientRepository.Add(domain);
                repository.Save();

                response.AddSuccess(Constants.CLIENT_SAVED, "Client added succesfully");
            }
            catch (Exception e)
            {
                ExceptionUtils.HandleGeneralError(response, logger, e);
            }

            return(response);
        }