public async Task <CommandResult> ExecuteAsync(int catId) { try { CsmsCategory cat = await _categoryRepository.GetByIdAsync(catId); if (cat != null) { cat.Deleted = true; await _categoryRepository.UpdateAsync(cat); return(CommandResult.Success); } return(CommandResult.Failed(new CommandResultError { Code = (int)HttpStatusCode.NotFound, Description = MessageError.NotFound })); } catch (Exception) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = MessageError.InternalServerError })); } }
public async Task <CommandResult> ExecuteAsync(CsmsCategory model) { try { if (model.Name == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.BadRequest, Description = MessageError.SomeDataEmptyOrInvalid })); } CsmsCategory cat = await _categoryRepository.GetByIdAsync(model.Id); if (model.Id != 0 && cat == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotFound, Description = MessageError.NotFound })); } cat = cat ?? new CsmsCategory(); cat.Name = model.Name; cat.Enabled = model.Enabled; if (cat.Id == 0) { await _categoryRepository.InsertAsync(cat); } else { await _categoryRepository.UpdateAsync(cat); } return(CommandResult.SuccessWithData(cat)); } catch (Exception) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = MessageError.InternalServerError })); } }
public async Task <CommandResult> ExecuteAsync(int categoryId, IFormFile file) { try { if (file == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File is null" })); } if (file.Length > 4000000) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File size must less than 4Mb" })); } if (!file.ContentType.Contains("image/")) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotAcceptable, Description = "File not support" })); } var names = file.FileName.Split('.'); var fileExtend = names[names.Length - 1]; byte[] fileContent; using (Stream stream = file.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { fileContent = binaryReader.ReadBytes((int)file.Length); } } CsmsCategory entity = await _categoryRepository.GetByIdAsync(categoryId) ?? new CsmsCategory(); entity.PhotoSize = file.Length.ToString(); entity.PhotoType = file.ContentType; entity.Photo = fileContent; entity.PhotoName = DateTime.Now.ToString(DateFormat.DateTimeFormatyyyyMMdd_hhmmss) + "." + fileExtend; if (entity.CategoryId == 0) { entity.CategoryId = categoryId; await _categoryRepository.InsertAsync(entity); } else { await _categoryRepository.UpdateAsync(entity); } return(CommandResult.Success); } catch (Exception) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.InternalServerError, Description = Message.InternalServerError })); } }
public async Task <IActionResult> SaveProductAsync(CsmsCategory model) { var result = await _saveCategoryCommand.ExecuteAsync(model); return(StatusCode(result.GetStatusCode(), result.GetData())); }