public async Task <ActionResult> DeleteVehicleOwner(int id, int ownerId)
        {
            var spec    = new VehiclesSpecification(id);
            var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

            var owner = vehicle.VehicleOwners.SingleOrDefault(x => x.OwnerId == ownerId);

            if (owner != null)
            {
                vehicle.RemoveOwner(ownerId);

                _unitOfWork.Repository <Vehicle>().Update(vehicle);
            }
            else
            {
                return(BadRequest(new ApiResponse(400, "Owner does not exist")));
            }

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem deleting owner vehicle")));
            }

            return(Ok());
        }
        public async Task <ActionResult <VehicleToReturnDto> > AddVehicleOwner(int id, OwnerCreateDto ownerCreateDto)
        {
            var validator = new OwnerValidator();
            var res       = validator.Validate(ownerCreateDto);

            if (res.IsValid)
            {
                var owner   = _mapper.Map <OwnerCreateDto, Owner>(ownerCreateDto);
                var spec    = new VehiclesSpecification(id);
                var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

                _unitOfWork.Repository <Owner>().Add(owner);
                var result = await _unitOfWork.Complete();

                if (result <= 0)
                {
                    return(BadRequest(new ApiResponse(400, "Problem creating owner")));
                }

                vehicle.AddOwner(owner.Id, id);
                _unitOfWork.Repository <Vehicle>().Update(vehicle);
                var result2 = await _unitOfWork.Complete();

                if (result2 <= 0)
                {
                    return(BadRequest(new ApiResponse(400, "Problem adding owner vehicle")));
                }

                return(_mapper.Map <Vehicle, VehicleToReturnDto>(vehicle));
            }
            else
            {
                return(BadRequest(res.Errors));
            }
        }
        public async Task <ActionResult> DeleteVehiclePhoto(int id, int photoId)
        {
            var spec    = new VehiclesSpecification(id);
            var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

            var photo = vehicle.Photos.SingleOrDefault(x => x.Id == photoId);

            if (photo != null)
            {
                if (photo.IsMain)
                {
                    return(BadRequest(new ApiResponse(400,
                                                      "You cannot delete the main photo")));
                }

                _photoService.DeleteFromDisk(photo);
            }
            else
            {
                return(BadRequest(new ApiResponse(400, "Photo does not exist")));
            }

            vehicle.RemovePhoto(photoId);

            _unitOfWork.Repository <Vehicle>().Update(vehicle);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem deleting photo vehicle")));
            }

            return(Ok());
        }
        public async Task <ActionResult <VehicleToReturnDto> > AddVehiclePhoto(int id, [FromForm] VehiclePhotoDto photoDto)
        {
            var spec    = new VehiclesSpecification(id);
            var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

            if (photoDto.Photo.Length > 0)
            {
                var photo = await _photoService.SaveToDiskAsync(photoDto.Photo);

                if (photo != null)
                {
                    vehicle.AddPhoto(photo.PictureUrl, photo.FileName);

                    _unitOfWork.Repository <Vehicle>().Update(vehicle);

                    var result = await _unitOfWork.Complete();

                    if (result <= 0)
                    {
                        return(BadRequest(new ApiResponse(400, "Problem adding photo product")));
                    }
                }
                else
                {
                    return(BadRequest(new ApiResponse(400, "problem saving photo to disk")));
                }
            }

            return(_mapper.Map <Vehicle, VehicleToReturnDto>(vehicle));
        }
        public async Task <ActionResult <VehicleToReturnDto> > GetVehicle(int id)
        {
            var spec = new VehiclesSpecification(id);

            var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

            if (vehicle == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            return(_mapper.Map <Vehicle, VehicleToReturnDto>(vehicle));
        }
        public async Task <ActionResult <Pagination <VehicleToReturnDto> > > GetVehicles(
            [FromQuery] VehicleSpecParams vehicleParams)
        {
            var spec = new VehiclesSpecification(vehicleParams);

            var countSpec = new VehicleWithFiltersForCountSpecification(vehicleParams);

            var totalItems = await _unitOfWork.Repository <Vehicle>().CountAsync(countSpec);

            var vehicles = await _unitOfWork.Repository <Vehicle>().ListAsync(spec);

            var data = _mapper.Map <IReadOnlyList <Vehicle>, IReadOnlyList <VehicleToReturnDto> >(vehicles);

            return(Ok(new Pagination <VehicleToReturnDto>(vehicleParams.PageIndex,
                                                          vehicleParams.PageSize, totalItems, data)));
        }
        public async Task <ActionResult <VehicleToReturnDto> > SetMainPhoto(int id, int photoId)
        {
            var spec    = new VehiclesSpecification(id);
            var vehicle = await _unitOfWork.Repository <Vehicle>().GetEntityWithSpec(spec);

            if (vehicle.Photos.All(x => x.Id != photoId))
            {
                return(NotFound());
            }

            vehicle.SetMainPhoto(photoId);

            _unitOfWork.Repository <Vehicle>().Update(vehicle);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem setting main photo vehicle")));
            }

            return(_mapper.Map <Vehicle, VehicleToReturnDto>(vehicle));
        }