public static UserTimeLog_DTO Add(int userId) { if (!User_DTO.Exists(userId)) return null; try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var userLog = new UserTimeLog_DTO() { FirstLogDate = DateTime.Now, LastLogDate = DateTime.Now, Duration = 0, UserId = userId }; context.UserLogs.Add(userLog); context.SaveChanges(); return GetLast(userId); } } catch (Exception ex) { throw ex; } }
public static UserTimeLog_DTO Update(UserTimeLog_DTO userLog) { Mapper.CreateMap<Models.UserTimeLog_DTO, Models.UserTimeLog_DTO>() .ForMember(dest => dest.Id, opt => opt.Ignore()); using (HomesteadViewerContext context = new HomesteadViewerContext()) { try { var c = Get(userLog.Id); if (c == null) throw new Exception("User Could Not Be Found"); Mapper.Map<Models.UserTimeLog_DTO, Models.UserTimeLog_DTO>(userLog, c); TimeSpan span = c.LastLogDate - c.FirstLogDate; c.Duration = (int)span.TotalMilliseconds; context.UserLogs.Attach(c); context.Entry(c).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return Get(userLog.Id); } catch (Exception ex) { throw ex; } } }