public async Task <IActionResult> CreateMessage(int userId,
                                                        MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> LikeUser(int id, int recipientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _repo.GetLike(id, recipientId);

            if (like != null)
            {
                return(BadRequest("You already like this user"));
            }

            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }

            like = new Like
            {
                LikerId = id,
                LikeeId = recipientId
            };

            _repo.Add <Like>(like);

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

            return(BadRequest("Failed to like user"));
        }
Ejemplo n.º 3
0
 public IActionResult Create(CarCreateViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         Car newcar = new Car
         {
             RegistrationNumber = viewModel.RegistrationNumber,
             Make              = viewModel.Make,
             Model             = viewModel.Model,
             ProductionYear    = viewModel.ProductionYear,
             Id                = new Guid(),
             IsAvailable       = true,
             IsReserved        = false,
             BodyType          = viewModel.BodyType,
             Color             = viewModel.Color,
             EngineSize        = viewModel.EngineSize,
             FuelType          = viewModel.FuelType,
             GearboxType       = viewModel.GearboxType,
             Milage            = viewModel.Milage,
             NextTechCheckDate = viewModel.RegistrationDate.AddYears(3),
             PowerHP           = viewModel.PowerHP,
             PowerkW           = viewModel.PowerkW,
             RegistrationDate  = viewModel.RegistrationDate,
             ServiceInterval   = viewModel.ServiceInterval,
             SpecType          = viewModel.SpecType,
             VIN               = viewModel.VIN,
             NextServiceMilage = viewModel.ServiceInterval,
         };
         _carsrepository.Add(newcar);
         return(RedirectToAction("details", new { id = newcar.Id }));
     }
     return(View());
 }
        internal bool AddNewCar(CarsAddVM viewModel)
        {
            Cars car = new Cars();

            if (!carRepository.CheckIfRegistrationNumberAlreadyExists(viewModel.Registrationnumber))
            {
                car.CarType            = viewModel.CarType;
                car.Kilometer          = viewModel.Kilometer;
                car.AvailableForRent   = true;
                car.Registrationnumber = viewModel.Registrationnumber;

                if (carRepository.Add(car).Id > 0)
                {
                    eventsService.CreateAddedCarEvent(car);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <Car> > PostCar([FromBody] Car car)
        {
            if (car == null)
            {
                return(BadRequest());
            }

            _repo.Add(car);
            await _repo.SaveAll();

            return(CreatedAtRoute("GetCar", new { id = car.Id }, car));
        }
Ejemplo n.º 6
0
        public Response AddCars(Cars car)
        {
            Response response = new Response();

            if (carsRepository.GetAll().Any(t => t.Vin == car.Vin))
            {
                response.Success = false;
                response.Message = "ERROR: Vin already exists";
            }
            if (bodyStyleRepository.GetAll().All(c => c.BodyStyleId != car.BodyStyleId))
            {
                response.Success = false;
                response.Message = "ERROR: no BodyStyles have id=" + car.BodyStyleId;
            }

            else if (carModelRepository.GetAll().All(c => c.CarModelId != car.CarModelId))
            {
                response.Success = false;
                response.Message = "ERROR: no CarModel have id=" + car.CarId;
            }
            else if (carTypeRepository.GetAll().All(c => c.CarTypeId != car.CarTypeId))
            {
                response.Success = false;
                response.Message = "ERROR: no CarType have id=" + car.CarId;
            }
            else if (colorRepository.GetAll().All(c => c.ColorId != car.ColorId))
            {
                response.Success = false;
                response.Message = "ERROR: no Colors have id=" + car.ColorId;
            }
            else if (transmissionRepository.GetAll().All(c => c.TransmissionId != car.TransmissionId))
            {
                response.Success = false;
                response.Message = "ERROR: no Transmission have id=" + car.TransmissionId;
            }
            else if (interiorColorRepository.GetAll().All(c => c.InteriorColorId != car.InteriorColorId))
            {
                response.Success = false;
                response.Message = "ERROR: no InteriorColor have id=" + car.InteriorColorId;
            }
            else
            {
                carsRepository.Add(car);
                response.Success = true;
                response.Message = "Car added";
            }
            return(response);
        }
Ejemplo n.º 7
0
        public async Task <Guid> Handle(CreateCarCommand command, CancellationToken cancellationToken)
        {
            var car = new Car
                      (
                command.BrandId,
                command.ModelName,
                command.ChassisTypeId,
                command.SeatsCount,
                command.Url,
                command.UrlImage
                      );

            await _carsRepo.Add(car);


            await _carsRepo.UnitOfWork.SaveChangesAsync();

            return(car.Id);
        }
Ejemplo n.º 8
0
        public void CheckIfAddingMethodIsSettingPropertiesCorrectly()
        {
            //Assign
            var car = new Car
            {
                Id    = 10,
                Make  = "Audi",
                Model = "A4",
                Year  = 1999
            };

            //Act
            carsData.Add(car);

            //Assert
            Assert.AreEqual(10, car.Id);
            Assert.AreEqual("Audi", car.Make);
            Assert.AreEqual("A4", car.Model);
            Assert.AreEqual(1999, car.Year);
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> PostCar(CreateCarCommand command)
        {
            var car = new Car
                      (
                command.BrandId,
                command.ModelName,
                command.ChassisTypeId,
                command.SeatsCount,
                command.Url,
                command.UrlImage
                      );

            await _carsRepo.Add(car);

            //  await _context.SaveChangesAsync();
            await _carsRepo.UnitOfWork.SaveChangesAsync();

            var addedCar = await _carsRepo.Get(car.Id);

            _logger.LogWarning("Добавлен автомобиль {brand}, {model}", addedCar?.Brand?.Name, addedCar?.ModelName);

            return(CreatedAtAction("GetCar", new { id = car.Id }, car));
        }
Ejemplo n.º 10
0
        public async Task <CarsServiceResultDto> Add(CarDomain carDomain)
        {
            var cars = await _carsRepository.GetAll();

            var countBeforeAdding = cars.Count;

            if (IsExisted(carDomain, cars))
            {
                return(new CarsServiceResultDto
                {
                    IsSuccess = false,
                    Message = "Car is already in database"
                });
            }

            try
            {
                var countAfterAdding = await _carsRepository.Add(carDomain);

                var isSuccess = countAfterAdding > countBeforeAdding;

                return(new CarsServiceResultDto
                {
                    IsSuccess = isSuccess,
                    Message = isSuccess ? string.Empty : $@"Inserting car with id {carDomain.Id} failed"
                });
            }
            catch (Exception ex)
            {
                return(new CarsServiceResultDto
                {
                    IsSuccess = false,
                    Message = "Add operation failed"
                });
            }
        }
Ejemplo n.º 11
0
 public async Task Add(Car car)
 {
     await _carsRepository.Add(_mapper.Map <Domain.Car>(car));
 }
Ejemplo n.º 12
0
 public void Add(Cars car)
 {
     carsRepository.Add(car);
 }
Ejemplo n.º 13
0
 public async Task <int> Add(Car item)
 {
     ValidateItem(item);
     return(await carsRepository.Add(item));
 }
Ejemplo n.º 14
0
 public Car Add(Car car)
 {
     return(carsRepository.Add(car));
 }