/// <summary> /// 更新 /// </summary> /// <param name="model">The model.</param> /// <returns> /// <c>true</c> if XXXX, <c>false</c> otherwise. /// </returns> /// <exception cref="System.ArgumentException">model</exception> public async Task <IResult> UpdateAsync(FooObject model) { var stepName = $"{nameof(FooService)}.UpdateAsync"; using (ProfilingSession.Current.Step(stepName)) { var startTime = SystemTime.UtcNow; if (model.EqualNull()) { throw new ArgumentNullException(nameof(model)); } IResult result = new Result(false); var exists = await this.IsExistsAsync(model.FooId); if (exists.Equals(false)) { result.Message = "無法找到 id 對應的資料"; return(result); } var fooModel = this.Mapper.Map <FooObject, FooModel>(model); result = await this.FooRepository.UpdateAsync(fooModel); return(result); } }
//----------------------------------------------------------------------------------------- /// <summary> /// 新增 /// </summary> /// <param name="model">The model.</param> /// <returns> /// <c>true</c> if XXXX, <c>false</c> otherwise. /// </returns> /// <exception cref="System.ArgumentNullException">model</exception> public async Task <IResult> InsertAsync(FooObject model) { var stepName = $"{nameof(FooService)}.InsertAsync"; using (ProfilingSession.Current.Step(stepName)) { var startTime = SystemTime.UtcNow; if (model.EqualNull()) { throw new ArgumentNullException(nameof(model)); } // 檢查名稱是否與現有資料衝突 var totalCount = await this.FooRepository.GetTotalCountAsync(true); if (totalCount > 0) { var collection = await this.FooRepository.GetCollectionAsync(1, totalCount, true); if (collection.Any(x => x.Name.Equals(model.Name, StringComparison.OrdinalIgnoreCase))) { return(new Result(false) { Message = "已有重複名稱的資料存在" }); } } if (model.FooId.Equals(Guid.Empty)) { model.FooId = Guid.NewGuid(); } // 新增資料 var fooModel = this.Mapper.Map <FooObject, FooModel>(model); var result = await this.FooRepository.InsertAsync(fooModel); return(result); } }