public ActionResult AddCategory(int uid) { try { Categories cat = new Categories { CName = Request["cname"], UID = uid }; CategoryValidation albumValidation = new CategoryValidation(); ValidationResult validationResult = albumValidation.Validate(cat); string Msg = ""; if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { Msg += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } return(Json(new { Result = "Error", Message = Msg })); } if (_repository._CategoriesRepositories.CategoryExist(cat.CName, uid) == false) { return(Json(new { Result = "Error", Message = "类别已存在" })); } int cid = _repository._CategoriesRepositories.AddCategories(cat); return(Json(new { Result = "OK", Message = cid })); } catch (Exception ex) { return(Json(new { Result = "Error", Message = ex.Message })); } }
public ActionResult UpdateCategory(int cid, string cname, int uid) { var u = _repository._UserRepositories.GetUserByUID(new Users { UID = uid }); if (u != null) { if (u.UserName != HttpContext.User.Identity.Name) { return(null); } else { try { Categories category = new Categories { CID = cid, CName = cname, UID = uid }; CategoryValidation albumValidation = new CategoryValidation(); ValidationResult validationResult = albumValidation.Validate(category); string Msg = ""; if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { Msg += "Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage; } return(Json(new { Result = "Error", Message = Msg })); } _repository._CategoriesRepositories.UpdateCategory(category); return(Json(new { Result = "OK" })); } catch (Exception ex) { return(Json(new { Result = "Error", Message = ex.Message })); } } } else { return(null); } }
private bool CategoryFillEntity() { this.CategoryItem.Name = this.cboCategory.Text; CategoryValidation validator = new CategoryValidation(); ValidationResult results = validator.Validate(this.CategoryItem); IList <ValidationFailure> failures = results.Errors; if (!(results.IsValid)) { foreach (ValidationFailure failure in failures) { MessageBox.Show(failure.ErrorMessage, "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } } return(true); }
private bool UpdateFillEntity() { this.Category = new Category { AppId = this.appId, Name = this.textBox1.Text }; CategoryValidation validator = new CategoryValidation(); ValidationResult results = validator.Validate(Category); IList <ValidationFailure> failures = results.Errors; if (!(results.IsValid)) { foreach (ValidationFailure failure in failures) { MessageBox.Show(failure.ErrorMessage, "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } } return(true); }
public ActionResult <Consumption> ModifyCategory(int id, CategoryRequest request) { string loggedUser = User.FindFirstValue("userId"); if (loggedUser == null) { return(Unauthorized()); } ValidationResult result = validator.Validate(request); if (!result.IsValid) { return(BadRequest(new ValidationSummary(result))); } Category updatedCategory = null; try { updatedCategory = categoriesService .ModifyCategory(request, id); } catch (SkrillaApiException e) { if (e.Code == "404") { return(NotFound()); } else { return(BadRequest(e.Message)); } } return(Ok(updatedCategory)); }