Ejemplo n.º 1
0
 public ActionResult CreateOrEdit(TimelineViewModel model)
 {
     if (ModelState.IsValid)
     {
         var db = AppUtilities.CurrentDbContext;
         Timeline entity;
         bool isCreate = model.Id == null;
         if (isCreate)
         {
             entity = new Timeline();
             db.Timelines.Add(entity);
             entity.CreatorId = AppUtilities.GetCurrentUserIdRequired();
             entity.CreationTimePoint = DateTime.UtcNow;
         }
         else
         {
             long idNonNull = (long)model.Id;
             entity = CommonUtilities.UniqueSingleOrDefault(db.Timelines.Where(e => e.Id == idNonNull));
             if (entity == null)
             {
                 model.Id = null;
                 return View(model);
             }
             db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
             // Only ID and non-updatable properties can change here, we simply use EntityToModel to reduce duplicate code and increase maintainability.
             EntityToModel(entity, model);
         }
         db.SaveChanges();
         if (isCreate)
         {
             model.Id = entity.Id;
         }
     }
     return View(model);
 }
Ejemplo n.º 2
0
 private static TimelineViewModel EntityToModel(Timeline entity, TimelineViewModel model)
 {
     model.CreationTimePoint = entity.CreationTimePoint;
     model.Id = entity.Id;
     model.Name = entity.Name;
     return model;
 }
Ejemplo n.º 3
0
 public ActionResult CreateOrEdit(long? id)
 {
     TimelineViewModel model = null;
     if (id != null)
     {
         var db = AppUtilities.CurrentDbContext;
         long idNonNull = (long)id;
         var entity = CommonUtilities.UniqueSingleOrDefault(db.Timelines.Where(e => e.Id == idNonNull));
         if (entity == null)
         {
             throw new ArgumentException();
         }
         model = new TimelineViewModel();
         EntityToModel(entity, model);
     }
     else
     {
         model = GetDefaultTimelineForCreate();
     }
     return View(model);
 }