Exemplo n.º 1
0
        public void Execute(int search, VehicleRequestDTO request)
        {
            var vehicle = AiContext.Vehicles
                          .Find(search);

            if (vehicle == null || vehicle.IsDeleted == 1)
            {
                throw new EntityNotFoundException("Vehicle");
            }
            var existingBrand = AiContext.Brands
                                .Find(request.BrandId);

            if (existingBrand == null)
            {
                throw new EntityNotFoundException("Brand");
            }
            var existingVehType = AiContext.VehicleTypes
                                  .Find(request.VehicleTypeId);

            if (existingVehType == null)
            {
                throw new EntityNotFoundException("Vehicle type");
            }
            vehicle.Automatic        = request.Automatic.HasValue ? request.Automatic.Value : false;
            vehicle.Model            = request.Model;
            vehicle.CostPerDay       = request.CostPerDay;
            vehicle.BrandId          = request.BrandId;
            vehicle.VehicleTypeId    = request.VehicleTypeId;
            vehicle.FuelTankCapacity = request.FuelTankCapacity;
            vehicle.Color            = request.Color;
            vehicle.ModifiedAt       = DateTime.Now;
            AiContext.SaveChanges();
        }
Exemplo n.º 2
0
        public IHttpActionResult RegisterVehicle([FromBody, Required] VehicleRequestDTO request)
        {
            VehicleManager     _vehicleManager = new VehicleManager();
            ResponseDTO <bool> response        = _vehicleManager.StoreVehicle(request);

            if (response.Data)
            {
                return(Ok(response.Data));
            }
            else
            {
                ResponseDTO <HttpStatusCode> statuesResponse = ResponseManager.ConvertErrorToStatus(response.Error);
                return(Content(statuesResponse.Data, statuesResponse.Error));
            }
        }
 public IActionResult Put(int id, [FromBody] VehicleRequestDTO value)
 {
     try
     {
         _updateVehicle.Execute(id, value);
         return(NoContent());
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Server error"));
     }
 }
 public ActionResult <VehicleResponseDTO> Post([FromBody] VehicleRequestDTO value)
 {
     try
     {
         var vehicle = _insertVehicle.Execute(value);
         return(Created("api/vehicles/" + vehicle.Id, vehicle));
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(e.Message));
     }
     catch (Exception)
     {
         return(StatusCode(500, "Server error"));
     }
 }
Exemplo n.º 5
0
        public VehicleResponseDTO Execute(VehicleRequestDTO request)
        {
            var vehicle = new Vehicle();
            var brand   = AiContext.Brands
                          .Find(request.BrandId);

            if (brand == null)
            {
                throw new EntityNotFoundException("Brand");
            }
            vehicle.BrandId = request.BrandId;
            var vehicleType = AiContext.VehicleTypes
                              .Find(request.VehicleTypeId);

            if (vehicleType == null)
            {
                throw new EntityNotFoundException("Vehicle type");
            }
            vehicle.Automatic        = request.Automatic.HasValue ? request.Automatic.Value : false;
            vehicle.VehicleTypeId    = request.VehicleTypeId;
            vehicle.Model            = request.Model;
            vehicle.CostPerDay       = request.CostPerDay;
            vehicle.FuelTankCapacity = request.FuelTankCapacity;
            vehicle.Color            = request.Color;
            AiContext.Vehicles.Add(vehicle);
            AiContext.SaveChanges();
            return(new VehicleResponseDTO
            {
                Id = vehicle.Id,
                Model = vehicle.Model,
                CostPerDay = vehicle.CostPerDay,
                Color = vehicle.Color,
                FuelTankCapacity = vehicle.FuelTankCapacity,
                VehicleType = vehicle.VehicleType.Name,
                VehicleBrand = vehicle.Brand.Name,
                Automatic = vehicle.Automatic,
                Rented = vehicle.Rented
            });
        }
Exemplo n.º 6
0
        public ResponseDTO <bool> StoreVehicle(VehicleRequestDTO request)
        {
            // Check if sessionId is in Guid Format
            Guid sessionId;

            try
            {
                sessionId = Guid.Parse(request.SessionId);
            }
            catch (Exception)
            {
                return(new ResponseDTO <bool>()
                {
                    Data = false,
                    Error = "SessionId is not a valid Guid"
                });
            }

            ResponseDTO <Session> sessionDTO = _sessionServices.GetSession(sessionId);

            // If session data is null, then an error occured
            if (sessionDTO.Data == null)
            {
                return(new ResponseDTO <bool>()
                {
                    Data = false,
                    Error = sessionDTO.Error
                });
            }

            // Input Validation
            if (request.Make == null || request.Model == null || request.Year == null || request.Plate == null || request.State == null || request.Vin == null)
            {
                return(new ResponseDTO <bool>()
                {
                    Data = false,
                    Error = "Vehicle object not fully defined."
                });
            }

            // Check if year is an integer
            int year;

            if (!int.TryParse(request.Year, out year))
            {
                return(new ResponseDTO <bool>()
                {
                    Data = false,
                    Error = "Year is not an integer."
                });
            }

            // Create the Vehicle to be stored
            Vehicle userVehicle = new Vehicle()
            {
                OwnerId = sessionDTO.Data.UserAccount.Id,
                Make    = request.Make,
                Model   = request.Model,
                Year    = year,
                Plate   = request.Plate,
                State   = request.State,
                Vin     = request.Vin
            };

            _loggerService.LogAction(LogConstants.ACTION_ADD_VEHICLE, sessionDTO.Data.Id.ToString(), sessionDTO.Data.SessionId.ToString());
            return(_vehicleServices.StoreVehicle(userVehicle));
        }