public Models.Aspect Create(Models.Aspect entity) { try { entity = AspectDataProccessor.ValidationAndProcess(entity); if (((new RestfulModels.Aspect()).Search(new Models.AspectSearchModel() { CategoryId = entity.CategoryId, Title = entity.Title })).Count() > 0) { var e = new RestfulModels.ValidationException(); e.ValidationErrors.Add("已经存在"); throw e; } DbEntities.Aspects.AddObject(entity); DbEntities.SaveChanges(); DbEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entity); return(entity); } catch { throw; } }
public static IList <RestfulJsonProccessor.RestfulOption> ControlProcess(Models.Aspect data, string userName) { try { return(null); } catch { return(null); } }
public Models.Review Create(Models.Review entity) { try { entity = ReviewDataProccessor.ValidationAndProcess(entity); DbEntities.Reviews.AddObject(entity); DbEntities.SaveChanges(); DbEntities.Refresh(System.Data.Objects.RefreshMode.StoreWins, entity); try { if (!string.IsNullOrEmpty(entity.RatedAspects)) { string[] aspects = Utilities.StringUtility.DeGroup(entity.RatedAspects, ';'); for (int i = 0; i < aspects.Count(); i++) { string[] aspectData = Utilities.StringUtility.DeGroup(aspects[i], '|'); Models.Aspect theAspect = (new RestfulModels.Aspect()).Read(int.Parse(aspectData[0]), entity.Creator); theAspect.RatedTimes++; (new RestfulModels.Aspect()).Update(theAspect.Id, theAspect, theAspect.Creator); (new RestfulModels.Rate()).Create(new Models.Rate() { ReviewId = entity.Id, AspectId = theAspect.Id, AspectTitle = theAspect.Title, Stars = int.Parse(aspectData[2]), Creator = entity.Creator, Created = entity.Created }, entity.Creator ); } } } catch { ; } return(Read(entity.Id)); } catch { throw; } }
public static Models.Aspect ValidationAndProcess(Models.Aspect data) { try { ValidationException validationException = new ValidationException(); try { if (data.CategoryId == null || (data.CategoryId < 1) || (new RestfulModels.Category()).Read((int)data.CategoryId) == null) { validationException.ValidationErrors.Add("产品类别为空"); } } catch { validationException.ValidationErrors.Add("产品类别为空"); } if (string.IsNullOrEmpty(data.Title)) { validationException.ValidationErrors.Add("评价内容为空"); } if (data.RatedTimes == null || data.RatedTimes < 0) { data.RatedTimes = 0; } //created if (data.Created == null) { data.Created = DateTime.Now; } if (validationException.ValidationErrors.Count() > 0) { throw validationException; } else { return(data); } } catch { throw; } }
public Models.Aspect Update(int id, Models.Aspect entity) { try { entity = AspectDataProccessor.ValidationAndProcess(entity); entity.Id = id; DbEntities.ApplyCurrentValues("Aspects", entity); DbEntities.SaveChanges(); return(Read(entity.Id)); } catch { throw; } }
public static bool Pass(byte action, Models.Aspect Aspect, string accessor = null) { try { if (action == RestfulAction.Create) { if (!string.IsNullOrEmpty(accessor) && (new Models.AccountMembershipService()).Exist(accessor)) { return(true); } else { return(false); } } else if (action == RestfulAction.Read) { return(true); } else if (action == RestfulAction.Update) { if (!string.IsNullOrEmpty(accessor) && Aspect.Creator == accessor) { return(true); } else { return(false); } } else if (action == RestfulAction.Delete) { return(false); } else { return(false); } } catch { return(false); } }
public Models.Aspect Create(Models.Aspect entity, string userName) { try { if (!AspectAccessControl.Pass(RestfulAction.Create, entity, userName)) { throw new NoAccessException("No Access"); } entity.Creator = userName; entity.Created = DateTime.Now; return(RestfulAspect.Create(entity)); } catch { throw; } }
private static object DataProcess(Models.Aspect data, string userName) { try { return(new { Id = data.Id, CategoryId = data.CategoryId, Title = data.Title, RatedTimes = data.RatedTimes, Creator = RestfulJsonProccessor.Account.MiniSingle(data.Creator), Created = data.Created.ToString(), Options = ControlProcess(data, userName) }); } catch { throw; } }
public Models.Aspect Update(int id, Models.Aspect entity, string userName) { try { var old = RestfulAspect.Read(id); if (!AspectAccessControl.Pass(RestfulAction.Update, old, userName)) { throw new NoAccessException("No Access"); } entity.CategoryId = old.CategoryId; entity.Title = old.Title; entity.Creator = userName; entity.Created = old.Created; return(RestfulAspect.Update(id, entity)); } catch { throw; } }
public ActionResult Create(Models.Aspect data) { try { return(Json ( new { Entity = RestfulJsonProccessor.Aspect.Single(RestfulAspect.Create(data, User.Identity.IsAuthenticated ? User.Identity.Name : null), User.Identity.IsAuthenticated ? User.Identity.Name : null) }, JsonRequestBehavior.AllowGet )); } catch (RestfulModels.NoAccessException) { Response.StatusCode = 401; return(null); } catch (RestfulModels.ValidationException e) { Response.StatusCode = 406; return(Json ( new { ValidationErrors = e }, JsonRequestBehavior.AllowGet )); } catch { Response.StatusCode = 500; return(null); } }
public Models.Review Update(int id, Models.Review entity) { try { entity = ReviewDataProccessor.ValidationAndProcess(entity); entity.Id = id; string oldRatedAspects = entity.RatedAspects; DbEntities.ApplyCurrentValues("Reviews", entity); DbEntities.SaveChanges(); try { if (!string.IsNullOrEmpty(entity.RatedAspects)) { string[] aspects = Utilities.StringUtility.DeGroup(entity.RatedAspects, ';'); for (int i = 0; i < aspects.Count(); i++) { string[] aspectData = Utilities.StringUtility.DeGroup(aspects[i], '|'); if ((";" + oldRatedAspects).IndexOf(";" + aspectData[0] + "|") < 0) { Models.Aspect theAspect = (new RestfulModels.Aspect()).Read(int.Parse(aspectData[0]), entity.Creator); theAspect.RatedTimes++; (new RestfulModels.Aspect()).Update(theAspect.Id, theAspect, theAspect.Creator); (new RestfulModels.Rate()).Create(new Models.Rate() { ReviewId = entity.Id, AspectId = theAspect.Id, AspectTitle = theAspect.Title, Stars = int.Parse(aspectData[2]), Creator = entity.Creator, Created = entity.Created }, entity.Creator ); } else { Models.Rate theRate = (new RestfulModels.Rate()).First(new Models.RateSearchModel() { ReviewId = entity.Id, AspectId = int.Parse(aspectData[0]) }, entity.Creator); (new RestfulModels.Rate()).Update( theRate.Id, new Models.Rate() { Stars = int.Parse(aspectData[2]), }, theRate.Creator ); } } } } catch { ; } return(Read(entity.Id)); } catch { throw; } }
public static object Single(Models.Aspect data, string userName) { return(DataProcess(data, userName)); }