public TopicController() { // initialize logic logic = new TopicAppLogic(); // assign contributor var utility = new ControllerUtilities(); contributor = utility.GetWebUserGuidFromCookies(); }
public Payload<Resource> Save(Resource obj) { // create payload var payload = new Payload<Resource>(); // todo: check security // Prep obj bool isNewAsset = (obj.Id <= 0); business.SetDefaults(ref obj); // Check if we need to create new subject foreach (var association in obj.TopicAssociations) { // Get from db and see if it already exists var subjectLogic = new TopicAppLogic(); if (association.Topic == null) { // check if exists var subjectIsNew = (subjectLogic.Get(association.Tag).Data == null); if (subjectIsNew) { // create new subject association.Topic = new Topic() { Tag = association.Tag, ContributorGuid = association.ContributorGuid, Contributor = association.Contributor, Created = DateTime.Now }; } } } // validate var rules = new ValidationRules(); rules.Validate(obj); // assign errors from validation (if applicable) payload.AssignValidationErrors(rules.Errors); // check if valid if (rules.IsValid) { // if existing asset, check the properties that have changed prior to update var changedProperties = new StringBuilder(); if (!isNewAsset) { var originalAsset = Get(obj.Id).Data; CheckChangedProperties(originalAsset, obj, ref changedProperties); } // save to db using (var queries = new ResourceQueries()) { queries.Save(ref obj); } // assign primary data payload.Data = obj; // log activity if (isNewAsset) { // new asset AuditUtilities.Log(obj.Contributor, ActivityEventItem.ResourceCreated, string.Format(Resources.AuditEntries.ResourceCreated, obj.Contributor.Username)); } else { // updated asset AuditUtilities.Log(obj.Contributor, ActivityEventItem.ResourceModified, string.Format(Resources.AuditEntries.ResourceModified, obj.Contributor.Username, changedProperties)); } } // todo: next steps in workflow // return payload return payload; }