public void Update(DatapointViewModel model) { var data = _context.AsQueryable <Data.Entities.Datapoint>().Where(x => x.ID == model.ID).FirstOrDefault(); if (data == null) { throw new ArgumentException("Data does not exists."); } //check if lob already exists if (data.Name != model.Name || data.SegmentID != model.SegmentID) { var dataExists = _context.AsQueryable <Data.Entities.Datapoint>().Where(x => x.Name == model.Name && x.SegmentID == model.SegmentID && x.Active == true).FirstOrDefault(); if (dataExists != null) { throw new ArgumentException("Datapoint name already exists in Segment."); } } data.Name = model.Name ?? data.Name; data.Datatype = model.Datatype ?? data.Datatype; data.SegmentID = model.SegmentID; data.SortOrder = model.SortOrder ?? data.SortOrder; data.ModifiedBy = model.ModifiedBy; data.DateModified = model.DateModified ?? DateTime.Now; data.Active = model.Active ?? data.Active; data.Visible = model.Visible ?? data.Visible; _context.Update <Data.Entities.Datapoint>(data); _context.SaveChanges(); }
public ActionResult Save(DatapointViewModel data) { string username = User.Identity.GetUserName(); CommonView model = new CommonView(); try { if (data.ID == 0) { DatapointViewModel segment = new DatapointViewModel(); //add data.CreatedBy = username; data.DateCreated = DateTime.Now; segment = _datapointService.Create(data); if (segment.ID > 0) { model.Message = "Data added."; } } else { //update data.DateModified = DateTime.Now; data.ModifiedBy = username; _datapointService.Update(data); model.Message = "Data updated."; } } catch (Exception ex) { if (ex.GetType() == typeof(ArgumentException)) { model.Message = ((ArgumentException)ex).Message; } else { model.Message = "Unable to save data. Please contact your system administrator."; } } return(Json(model, JsonRequestBehavior.AllowGet)); }
public DatapointViewModel Create(DatapointViewModel model) { var dataExists = _context.AsQueryable <Data.Entities.Datapoint>().Where(x => x.Name == model.Name && x.Active == true && x.SegmentID == model.SegmentID).FirstOrDefault(); if (dataExists != null) { throw new ArgumentException("Datapoint name already exists in segment."); } var sortOrder = 0; var lastData = _context.AsQueryable <Data.Entities.Datapoint>().Where(x => x.Active == true && x.SegmentID == model.SegmentID).OrderByDescending(x => x.SortOrder).FirstOrDefault(); if (lastData != null) { sortOrder = lastData.SortOrder + 1; } var data = new Data.Entities.Datapoint() { Name = model.Name, Datatype = model.Datatype, SegmentID = model.SegmentID, SortOrder = sortOrder, CreatedBy = model.CreatedBy, ModifiedBy = null, DateCreated = model.DateCreated ?? DateTime.Now, DateModified = null, Active = model.Active ?? true, Visible = model.Visible ?? true }; _context.Add <Data.Entities.Datapoint>(data); _context.SaveChanges(); model.ID = data.ID; model.Active = data.Active; model.Visible = data.Visible; return(model); }