public static async Task <ServiceResult> TryUpdate <T>(this IMongoCollection <T> db, DrinkViewModel viewModel, UpdateDefinition <T> updateDefinition) where T : IEntity { var result = ResultFactory.Create(); try { var checkByNameResult = await db.Find(GetByName <T>(viewModel.Name)).ToListAsync(); if (checkByNameResult.IsEmpty() == false) { var queryResult = await db.Find(GetById <T>(viewModel.Id)).ToListAsync(); if (queryResult.IsOneSelected()) { await db.FindOneAndUpdateAsync(GetById <T>(viewModel.Id), updateDefinition); result.Status = nameof(Status.Updated); } else { result.Status = nameof(Status.FindMoreThanOne); } } else { result.Status = nameof(Status.NotExist); } } catch (Exception ex) { result.AddError(ex.Message); result.Status = nameof(Status.OperationFailed); } return(result); }
public BaseResult <string> SaveFile(IFormFile file, string filename, string folder) { var result = ResultFactory.Create(string.Empty); if (file.ContentDisposition != null) { //parse uploaded file var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var orginalFilename = parsedContentDisposition.FileName.Trim('"'); var fileExtension = Path.GetExtension(orginalFilename); result.Object = filename + fileExtension; string uploadPath = Path.Combine(UploadDestination, folder, result.Object); //check extension bool extension = this.VerifyFileExtension(uploadPath); if (extension == false) { result.ResolveError("7000", "Desteklenmeyen bir resim dosyası seçtiniz. Lütfen .jpg, .png yada .gif formatında bir dosya seçiniz."); return(result); } //check file size bool filesize = this.VerifyFileSize(file); if (filesize == false) { result.ResolveError("7001", "Dosya boyutu 1mb tan fazla olamaz. Lütfen boyutu daha 1mb altında bir dosya seçiniz."); return(result); } //save the file to upload destination file.CopyTo(new FileStream(uploadPath, FileMode.Create)); } return(result); }
public async Task <ServiceResult> Register(RegisterViewModel viewModel) { var result = ResultFactory.Create(); var user = GetFromViewModel(viewModel); try { var dbResult = await userManager.CreateAsync(user, viewModel.Password); if (dbResult.Succeeded) { await signInManager.SignInAsync(user, false); result.Status = nameof(Status.Registred); return(result); } result.AddErrors(dbResult.Errors.Select(x => $"{x.Code}: {x.Description}").ToArray()); result.Status = nameof(Status.OperationFailed); return(result); } catch (Exception ex) { result.AddError(ex.Message); result.Status = nameof(Status.OperationFailed); } return(result); }
static void Main(string[] args) { Runtime runtime = new Runtime(QueueMode.Server | QueueMode.Client); ILoggerFactory loggerFactory = new LoggerFactory() .AddDebug(LogLevel.Debug) .AddNesterLog(LogLevel.Debug); ILogger logger = loggerFactory.CreateLogger <Bitbucket>(); BitbucketRepository repo = new BitbucketRepository(runtime, logger); var consumer = runtime.QueueServer .CreateConsumer(); consumer.Received += async(model, ea) => { var body = ea.Body; var props = ea.BasicProperties; string response = null; try { if (props.Headers != null && props.Headers.ContainsKey("Type")) { string messageType = Encoding.UTF8.GetString( props.Headers["Type"] as byte[]); if (messageType == typeof(SearchQuery).ToString()) { var message = Encoding.UTF8.GetString(body); var query = JsonConvert.DeserializeObject <SearchQuery>(message); SearchResult result = await repo.SearchAsync(query); logger.LogInformation("Query Id {0} term {1}", query.Id, query.Text); response = ResultFactory.CreateSingle <SearchResult>( result, 0, "Success"); } } } catch (Exception e) { logger.LogError(" [.] " + e.Message); response = ResultFactory.Create( -1, "Failed", e.Message); } finally { logger.LogInformation("CorrelationId {0}", props.CorrelationId); var responseBytes = Encoding.UTF8.GetBytes(response); runtime.QueueServer.DefaultChannel.Publish( props.ReplyTo, responseBytes, typeof(Result <SearchResult>), props.CorrelationId); } }; }
/// <summary> /// Middleware entry point. Called by Kestrel. /// </summary> public async Task InvokeAsync(HttpContext httpContext, IContext fnContext, IInput input) { var rawResult = await RunFunctionAsync(httpContext.RequestServices, fnContext, input); var result = ResultFactory.Create(rawResult); await result.WriteResult(httpContext.Response); }
public async Task InvokeAsync(HttpContext httpContext, IContext fnContext, IInput input) { var rawResult = await _function.InvokeAsync(fnContext, input); var result = ResultFactory.Create(rawResult); await result.WriteResult(httpContext.Response); }
public async Task <IHttpActionResult> Upload() { return(await ResultFactory.Create(ModelState, async arg => await Task.Factory.StartNew(() => { System.Web.UI.WebControls.FileUpload upload = new System.Web.UI.WebControls.FileUpload(); string name = upload.FileName; //获取文件名 //string name1 = upload.PostedFile.FileName; //获取完整客户端文件路径 string type = upload.PostedFile.ContentType; //上传文件类型 //string size = upload.PostedFile.ContentLength.ToString();//上传文件大小 //string type2 = name.Substring(name.LastIndexOf(".") + 1);//上传文件后缀名 string ipath = System.Web.HttpContext.Current.Server.MapPath("upload") + "\\" + name; //上传到服务器上后的路径(实际路径),"\\"必须为两个斜杠,在C#中一个斜杠表示转义符. string ipath1 = System.Web.HttpContext.Current.Server.MapPath("upload"); //创建文件夹时用 //string wpath = "upload\\" + name; //虚拟路径 if (type == "xls" || type == "xlsx" || type == "txt") //根据后缀名来限制上传类型 { if (!Directory.Exists(ipath1)) //判断文件夹是否已经存在 { Directory.CreateDirectory(ipath1); //创建文件夹 } upload.SaveAs(ipath); //上传文件到ipath这个路径里 } return true; }), "success", "未知")); }
public async Task <IHttpActionResult> GetRunLog(string stationId) { return(await ResultFactory.Create(ModelState, async arg => { var logs = await cameraStationRunLogService.GetManyAsync(l => l.CameraStationsSerialnum.Equals(stationId)); return new ApiResult(ExceptionCode.Success, "", logs); }, stationId, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> Delete(string modelId) { return(await ResultFactory.Create(ModelState, async arg => { var result = await AgrDiagnosticModelService.DeleteAsync(modelId) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, modelId, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> GetStationOnlineStatistics(string stationId) { return(await ResultFactory.Create(ModelState, async arg => { var statistics = await cameraStationOnlineStatisticsService.GetManyAsync(s => s.CameraStationsSerialnum.Equals(stationId)); return new ApiResult(ExceptionCode.Success, "", statistics); }, stationId, "success", "请检查请求参数")); }
public void CreateItemWithShowInMenuFalseShouldReturnEmptyString() { FieldList stubFieldList = new FieldList(); Item stub = new ContentItem(stubFieldList); string navigationTitle = ResultFactory.Create(stub); Assert.IsNotNull(navigationTitle); }
public async Task <IHttpActionResult> Update(string companyId) { return(await ResultFactory.Create(ModelState, async arg => { var result = await companyService.DeleteAsync(companyId) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, companyId, "success", "请检查请求参数")); }
public void CreateItemWithShowInMenuTrueShouldReturnItemNavigationTitle() { FieldList stubFieldList = new FieldList(); Item stub = new ContentItem(stubFieldList); string navigationTitle = ResultFactory.Create(stub); Assert.AreEqual("NavigationTitle", navigationTitle); }
public async Task <IHttpActionResult> Get(string stationId) { return(await ResultFactory.Create(ModelState, async arg => { var station = await areaStationService.GetByIdWithRedisAsync(stationId); return new ApiResult(ExceptionCode.Success, "", station); //return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", // null); }, stationId, "success", "请检查请求参数")); }
public void NotANumber() { IResultFactory resultFactory = new ResultFactory(); Result result = resultFactory.Create(ResultType.NotANumber); Assert.AreEqual(0, result.Input); Assert.AreEqual(0, result.Difference); Assert.AreEqual(ResultType.NotANumber, result.Type); }
public void NumberTooSmall() { IResultFactory resultFactory = new ResultFactory(); int min = (int)(Int32.MinValue); long input = min - 1; Result result = resultFactory.Create(ResultType.NumberTooSmall, input); Assert.AreEqual(input, result.Input); Assert.AreEqual(0, result.Difference); Assert.AreEqual(ResultType.NumberTooSmall, result.Type); }
public async Task <IHttpActionResult> GetDeviceType(string deviceId) { return(await ResultFactory.Create(ModelState, async arg => { var device = await deviceService.GetByIdAsync(deviceId); DeviceTypeDto deviceType = null; if (device != null) { deviceType = await deviceTypeService.GetByIdWithRedisAsync(device.DeviceTypeSerialnum); } return new ApiResult(ExceptionCode.Success, "", deviceType); }, deviceId, "success", "请检查请求参数")); }
public void ValidButNegativeNumber() { IResultFactory resultFactory = new ResultFactory(); long input = -1; int difference = 6; Result result = resultFactory.Create(ResultType.ValidButNegativeNumber, input, difference); Assert.AreEqual(input, result.Input); Assert.AreEqual(difference, result.Difference); Assert.AreEqual(ResultType.ValidButNegativeNumber, result.Type); }
public async Task <IHttpActionResult> UpdatePwd([FromBody] string userName, string pwd) { return(await ResultFactory.Create(ModelState, async arg => { var user = await SysUserService.GetAsync(u => u.UserName.Equals(userName)); user.Password = pwd; user.UpdateTime = DateTime.Now; var result = await SysUserService.UpdateAsync(user) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, userName, "success", "请检查请求参数")); }
public void NumberTooLarge() { IResultFactory resultFactory = new ResultFactory(); int max = (int)(Int32.MaxValue); long input = max + 1; Result result = resultFactory.Create(ResultType.NumberTooLarge, input); Assert.AreEqual(input, result.Input); Assert.AreEqual(0, result.Difference); Assert.AreEqual(ResultType.NumberTooLarge, result.Type); }
public async Task <IHttpActionResult> GetByUser(string userId) { return(await ResultFactory.Create(ModelState, async arg => { var companyUser = await companyUserService.GetBySysUserIdAsync(userId); CompanyDto company = null; if (companyUser != null) { company = await companyService.GetByIdAsync(companyUser.CompanySerialnum); } return new ApiResult(ExceptionCode.Success, "", company); }, userId, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> GetFacilityType(string facilityId) { return(await ResultFactory.Create(ModelState, async arg => { var facility = await facilityService.GetByIdWithRedisAsync(facilityId); FacilityTypeDto facilityType = null; if (facility != null) { facilityType = await facilityTypeService.GetByIdWithRedisAsync(facility.FacilityTypeSerialnum); } return new ApiResult(ExceptionCode.Success, "", facilityType); }, facilityId, "success", "请检查请求参数")); }
public void Valid() { IResultFactory resultFactory = new ResultFactory(); long input = 3; int difference = 2; Result result = resultFactory.Create(ResultType.Valid, input, difference); Assert.AreEqual(input, result.Input); Assert.AreEqual(difference, result.Difference); Assert.AreEqual(ResultType.Valid, result.Type); }
public async Task <IHttpActionResult> GetByUser(string userId) { return(await ResultFactory.Create(ModelState, async arg => { var companyUser = await companyUserService.GetBySysUserIdAsync(userId); IEnumerable <DeviceDto> devices = null; if (companyUser != null) { devices = await deviceService.GetDevicesByFacilityIdAsync(companyUser.FacilitySerialnum); } return new ApiResult(ExceptionCode.Success, "", devices.ToList()); //return devices.ToList(); }, userId, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> Update([FromBody] CompanyModel company) { return(await ResultFactory.Create(ModelState, async arg => { var result = await companyService.UpdateAsync(new CompanyDto { Name = company.Name, Pinyin = PinYin.GetFirst(company.Name).ToUpper(), Sort = 0, Status = 1, UpdateTime = DateTime.Now }) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, company, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> Update([FromBody] SysRoleModel role) { return(await ResultFactory.Create(ModelState, async arg => { var result = await sysRoleService.UpdateAsync(new SysRoleDto { Url = role.Url, Name = role.Name, UpdateTime = DateTime.Now }) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, role, "success", "请检查请求参数")); }
public static async Task <ServiceResult> TryDelete <T>(this IMongoCollection <T> db, string id) { var result = ResultFactory.Create(); try { await db.FindOneAndDeleteAsync(GetById <T>(id)); result.Status = nameof(Status.Removed); } catch (Exception ex) { result.AddError(ex.Message); result.Status = nameof(Status.OperationFailed); } return(result); }
public async Task <ServiceResult> LogOut() { var result = ResultFactory.Create(); try { await signInManager.SignOutAsync(); result.Status = nameof(Status.LoggedOut); return(result); } catch (Exception ex) { result.AddError(ex.Message); result.Status = nameof(Status.OperationFailed); } return(result); }
public async Task <IHttpActionResult> Update([FromBody] DeviceModel device) { return(await ResultFactory.Create(ModelState, async arg => { var result = await deviceService.UpdateAsync(new DeviceDto { DeviceTypeSerialnum = device.DeviceTypeSerialnum, FacilitySerialnum = device.FacilitySerialnum, Name = device.Name, UpdateTime = DateTime.Now }) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, device, "success", "请检查请求参数")); }
public async Task <IHttpActionResult> Update([FromBody] FacilityModel facitity) { return(await ResultFactory.Create(ModelState, async arg => { var result = await facilityService.UpdateAsync(new FacilityDto { FacilityTypeSerialnum = facitity.FacilityTypeSerialnum, FarmSerialnum = facitity.FarmSerialnum, Name = facitity.Name, UpdateTime = DateTime.Now }) > 0; return new ApiResult(result ? ExceptionCode.Success : ExceptionCode.InternalError, "", null); }, facitity, "success", "请检查请求参数")); }