public async Task <IActionResult> Post([FromBody] RetailerViewModel retailerVm) { if (retailerVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var newItem = Mapper.Map <Retailer>(retailerVm); _retailerRepository.SetPinyin(newItem); newItem.SetCreation(UserName); _retailerRepository.Add(newItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } var vm = Mapper.Map <RetailerViewModel>(newItem); return(CreatedAtRoute("GetRetailer", new { id = vm.Id }, vm)); }
public void Create(Retailer retailer) { try { _repository.Add(retailer); _domain.DoCommand(CommandType.Add, retailer); } catch (Exception ex) { _domain.DoCommand(CommandType.Exception, ex); } }
public async Task <bool> Handle(CreateRetailerCommand request, CancellationToken cancellationToken) { // Add new Retailer and add AddCreatedRetailerDomainEvent Retailer retailer = new Retailer(request.Name, request.Address); _retailerRepo.Add(retailer); // Publish integration event _eventBus.SendRetailer(retailer); // Save changes/Save changes async return(await _retailerRepo.SaveEntitiesAsync(cancellationToken)); }
public IHttpActionResult CreateRetailer([FromBody] Retailer retailerObj) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } dataRepository.Add(retailerObj); } catch (Exception ex) { throw ex; } return(Ok(retailerObj)); }
public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken) { if (request.Retailer == null || request.Retailer.Address == null || request.Retailer.Contact == null) { throw new BusinessException("AddWrongInformation"); } if (request.Retailer.ImageData?.Length > Constant.MaxImageLength) { throw new BusinessException("Image.OutOfLength"); } //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData. if (request.Retailer.ImageData?.Length > 200) { string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Retailer.ImageData)); if (!CommonHelper.IsImageType(type)) { throw new BusinessException("Image.WrongType"); } string Base64StringData = request.Retailer.ImageData.Substring(request.Retailer.ImageData.IndexOf(",") + 1); string fileName = Guid.NewGuid().ToString().Replace("-", ""); request.Retailer.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.RetailerImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData); } var rs = -1; using (var conn = DALHelper.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction()) { try { retailerRepository.JoinTransaction(conn, trans); locationRepository.JoinTransaction(conn, trans); request.Retailer = CreateBuild(request.Retailer, request.LoginSession); if (!request.Retailer.IsCompany) { request.Retailer.TaxCode = string.Empty; } request.Retailer.Id = await retailerRepository.Add(request.Retailer); request.Retailer.Address.Id = 0; request.Retailer.Address.ObjectType = LocationOjectType.R.ToString(); request.Retailer.Address.ObjectId = request.Retailer.Id; request.Retailer.Address.IsUsed = true; request.Retailer.Address = CreateBuild(request.Retailer.Address, request.LoginSession); var addressId = await locationRepository.AddOrUpdateAddress(request.Retailer.Address); request.Retailer.Contact.Id = 0; request.Retailer.Contact.ObjectType = LocationOjectType.R.ToString(); request.Retailer.Contact.ObjectId = request.Retailer.Id; request.Retailer.Contact.IsUsed = true; request.Retailer.Contact = CreateBuild(request.Retailer.Contact, request.LoginSession); var contactId = await locationRepository.AddOrUpdateContact(request.Retailer.Contact); request.Retailer.AddressId = addressId; request.Retailer.ContactId = contactId; rs = await retailerRepository.Update(request.Retailer); } catch (Exception ex) { LogHelper.GetLogger().Error(ex.Message); return(rs = -1); } finally { if (rs == 0) { trans.Commit(); } else { try { trans.Commit(); } catch { } CommonHelper.DeleteImage(request.Retailer.ImageURL); } } } }; return(rs); }