public static Control ToDataModel(this Contracts.Control.CreateControl create, RAAPEntities db) { var control = new Control { Name = create.Name, Description = create.Description, CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, ExecutedDate = create.ExecutedDate, Deadline = create.Deadline, InvestmentCost = create.InvestmentCost, LegalObligation = create.LegalObligation, MaintenanceCost = create.MaintenanceCost, ResponsibleUserId = create.ResponsibleUserId, Status = (int)create.Status, Type = (int)create.Type, ValidTo = create.ValidTo, ControlCategory = (create.Category != null ? db.ControlCategories.FirstOrDefault(cc => cc.ControlCategoryId == create.Category.ControlCategoryId) : null), React = create.React, Detect = create.Detect, Prevent = create.Prevent, AlertUserId = create.AlertUserId, AlertDate = create.AlertDate, Avoid = create.Avoid }; create.Evaluations.Where(e => e.EvaluationId == 0 && !string.IsNullOrEmpty(e.Text)).OrderBy(e => e.Revision) .ForEach(e => control.HtmlComments.Add(e.ToDataModel(db, control))); create.Risks.ForEach(r => control.ControlRisks.Add(r.ToDataModel())); return(control); }
public static void ApplyUpdate(this Control dataItem, Contracts.Control.UpdateControl update, RAAPEntities db) { dataItem.Name = update.Name; dataItem.Description = update.Description; dataItem.UpdatedOn = DateTime.Now; dataItem.ExecutedDate = update.ExecutedDate; dataItem.Deadline = update.Deadline; dataItem.InvestmentCost = update.InvestmentCost; dataItem.LegalObligation = update.LegalObligation; dataItem.MaintenanceCost = update.MaintenanceCost; dataItem.ResponsibleUserId = update.ResponsibleUserId; dataItem.Status = (int)update.Status; dataItem.Type = (int)update.Type; dataItem.ValidTo = update.ValidTo; dataItem.ControlCategory = db.ControlCategories.FirstOrDefault(c => c.ControlCategoryId == update.Category.ControlCategoryId); dataItem.React = update.React; dataItem.Detect = update.Detect; dataItem.Avoid = update.Avoid; dataItem.Prevent = update.Prevent; dataItem.AlertUserId = update.AlertUserId; dataItem.AlertDate = update.AlertDate; update.Evaluations.Where(e => e.EvaluationId == 0 && !string.IsNullOrEmpty(e.Text)).OrderBy(e => e.Revision) .ForEach(e => dataItem.HtmlComments.Add(e.ToDataModel(db, dataItem))); //Remove deleted var itemsForDelete = dataItem.ControlRisks.Where(r => update.Risks.All(rr => rr.RiskReduceId != r.ControlRiskId)).ToList(); itemsForDelete.ForEach(r => dataItem.ControlRisks.Remove(r)); //Update existing update.Risks.Where(r => r.RiskReduceId > 0).ForEach(risk => dataItem.ControlRisks.First(r => r.ControlRiskId == risk.RiskReduceId).UpdateFrom(risk)); //Add new update.Risks.Where(r => r.RiskReduceId <= 0).ForEach(risk => dataItem.ControlRisks.Add(risk.ToDataModel())); }
public static Contracts.Control.Control ToContract(this Control dataItem, UserService userService) { return(new Contracts.Control.Control { Avoid = dataItem.Avoid, Enabled = true, ControlId = dataItem.ControlId, Name = dataItem.Name, Description = dataItem.Description, CreatedOn = dataItem.CreatedOn, UpdatedOn = dataItem.UpdatedOn, ExecutedDate = dataItem.ExecutedDate, Deadline = dataItem.Deadline, InvestmentCost = dataItem.InvestmentCost, LegalObligation = dataItem.LegalObligation, MaintenanceCost = dataItem.MaintenanceCost, ResponsibleUserId = dataItem.ResponsibleUserId, Status = (ControlStatus)dataItem.Status, Type = (ControlType)dataItem.Type, ValidTo = dataItem.ValidTo, Category = dataItem.ControlCategory.ToContract(), ResponsibleUser = dataItem.ResponsibleUserId.HasValue ? userService.GetUserByUserId(dataItem.ResponsibleUserId.Value, false) : null, Detect = dataItem.Detect, Prevent = dataItem.Prevent, React = dataItem.React, AlertDate = dataItem.AlertDate, AlertUserId = dataItem.AlertUserId, AlertUser = dataItem.AlertUserId.HasValue ? userService.GetUserByUserId(dataItem.AlertUserId.Value, false) : null, Risks = dataItem.ControlRisks.Select(r => r.ToContract()).ToList(), Evaluations = dataItem.HtmlComments.Select(h => h.ToContract(userService)).OrderByDescending(h => h.Revision).ToList() }); }