예제 #1
0
        public async Task <ServiceResult> DeleteCarByIdAsync(Guid userId, Guid carId)
        {
            var car = await _carRepository.GetCarByIdAsync(carId);

            if (car == null)
            {
                return(FailResult(404, "Car doesn't exist"));
            }

            var userCar = car.UserCars.FirstOrDefault(u => u.UserId == userId);

            if (userCar == null || !userCar.UserCarRoleId.Equals("owner"))
            {
                return(FailResult(401, "User can't delete this car"));
            }

            if (!await _carRepository.DeleteCarAsync(car))
            {
                return(FailResult(500, "Failed while deleting car"));
            }

            return(SuccessResult(200, new DeleteCarResponse {
                CarDeleted = true
            }));
        }
예제 #2
0
        public async Task <ActionResult> DeleteCarAsync(int id)
        {
            var carModelFromRepository = await _repository.GetCarByIdAsync(id);

            if (carModelFromRepository == null)
            {
                return(NotFound());
            }

            await _repository.DeleteCarAsync(carModelFromRepository);

            return(NoContent());
        }
예제 #3
0
        public async Task <IActionResult> Delete(int carID)
        {
            Car currentCar = await _repository.FindCarAsync(carID);

            if (currentCar == null)
            {
                return(NotFound());
            }

            bool isCarAuthor = AuthorizeSucceeded(currentCar).Result;

            if (!isCarAuthor)
            {
                return(StatusCode(403));
            }

            await _repository.DeleteCarAsync(carID);

            return(new NoContentResult());
        }
예제 #4
0
        public async Task <IActionResult> RemoveItem(int CarID)
        {
            Car car = await _repository.FindCarAsync(CarID);

            bool isCarAuthor = AuthorizeSucceeded(CarID).Result;

            if (car == null)
            {
                return(StatusCode(404));
            }
            if (isCarAuthor)
            {
                await _repository.DeleteCarAsync(CarID);

                TempData["message"] = $"{car.Brand} {car.Model} was deleted!";
                return(RedirectToAction(nameof(List))); //its safety
            }
            else
            {
                return(StatusCode(403));
            }
        }
예제 #5
0
        public async Task <Car> DeleteCar(Car car)
        {
            var result = await _carRepository.DeleteCarAsync(car);

            return(result);
        }