public ResModel UpdateCarModel(CarModelDto carModelDto, UserDto operationUser) { using (var db = new ModelContext()) { var carModel = db.CarModel.FirstOrDefault(i => i.Id == carModelDto.Id); if (carModel == null) { return(new ResModel() { Msg = "更新车型信息失败,未找到该车型", Success = false }); } //判断车型全称是否有相同的,如果有,则存在相同的品牌名、系列名、型号名,即车型相同 if (db.CarModel.Any(i => i.FullName == carModelDto.FullName)) { return(new ResModel() { Msg = "更新车型信息失败,已存在相同车型信息", Success = false }); } var carBrand = db.CarBrand.FirstOrDefault(i => i.Name == carModelDto.BrandName) ?? new CarBrand() { Id = Guid.NewGuid(), Name = carModelDto.BrandName }; //判断同一个品牌下,是否存在该系列 var carSeries = db.CarSeries.FirstOrDefault(i => i.Name == carModelDto.SeriesName && i.Brand.Name == carModelDto.BrandName) ?? new CarSeries() { Id = Guid.NewGuid(), Name = carModelDto.SeriesName, BrandId = carBrand.Id }; carModel.Name = carModelDto.Name; carModel.Price = carModelDto.Price; carModel.FullName = carModelDto.FullName; carModel.Color = carModelDto.Color; carModel.SeriesId = carSeries.Id; using (var scope = new TransactionScope()) { try { db.CarBrand.AddOrUpdate(carBrand); db.CarSeries.AddOrUpdate(carSeries); db.SaveChanges(); scope.Complete(); } catch (Exception e) { return(new ResModel() { Msg = "更新车型信息失败", Success = false }); } return(new ResModel() { Msg = "更新车型信息成功", Success = true }); } } }
public async Task <ActionResult> Update([FromBody] CarModelDto carModelDto) { try { await _carModelService.UpdateAsync(carModelDto); return(Ok()); } catch (Exception e) { return(BadRequest(e)); } }
public async Task <ActionResult> Post([FromBody] CarModelDto carModelDto) { try { if (carModelDto == null) { return(BadRequest($"{nameof(carModelDto)} can not not be null!")); } await _carModelService.AddAsync(carModelDto); return(Ok()); } catch (Exception e) { return(BadRequest(e)); } }
public async Task CategoryServiceService_GetCarModelById_GetExactlyOneCarModelWithSpecifiedValues() { var modelToReturn = new CarModel { Id = Guid.Empty, Title = "Test", BrandId = Guid.Empty, CategoryId = Guid.Empty }; var expectedOjbect = new CarModelDto { Id = Guid.Empty, Title = "Test", BrandId = Guid.Empty, CategoryId = Guid.Empty }; var config = new MapperConfiguration(cfg => cfg.CreateMap <CarModel, CarModelDto>()); var repo = A.Fake <ICarModelRepository>(); var modelService = new CarModelService(repo, config.CreateMapper()); A.CallTo(() => repo.GetAsync(Guid.Empty)).Returns(modelToReturn); var result = await modelService.GetByIdAsync(Guid.Empty); Assert.That(result.CategoryId, Is.EqualTo(expectedOjbect.CategoryId)); Assert.That(result.BrandId, Is.EqualTo(expectedOjbect.BrandId)); Assert.That(result.Title, Is.EqualTo(expectedOjbect.Title)); Assert.That(result.Id, Is.EqualTo(expectedOjbect.Id)); }
public async Task UpdateAsync(CarModelDto obj) => await _repository.UpdateAsync(_mapper.Map <CarModelDto, CarModel>(obj));
public ResModel UpdateCarModel(CarModelDto carModelDto, UserDto operationUser) { return(_carModelRepository.UpdateCarModel(carModelDto, operationUser)); }
public ActionResult Update(CarModelDto carModelDto) { var currentUser = Session["LogUser"] as UserDto; return(Json(_carModelService.UpdateCarModel(carModelDto, currentUser))); }