public TEntryDto UpdateEntry(TContextDto context, TEntryDto entry) { try { if (entry.TEntryID > 0) { var existingEntry = _db.TEntries.SingleOrDefault(e => e.TEntryID == entry.TEntryID); if (existingEntry != null) { var calculator = new TracktorCalculator(context, _db); if (existingEntry.TTask.TProject.TUserID == context.TUserID) { if (entry.IsDeleted == true) { // stop if in progress if (!existingEntry.EndDate.HasValue) { StopTask(context, entry.TTaskID); existingEntry = _db.TEntries.SingleOrDefault(e => e.TEntryID == entry.TEntryID); } _db.TEntries.Remove(existingEntry); _db.SaveChanges(); return(null); } else { var startUtc = calculator.ToUtc(entry.StartDate).Value; var endUtc = calculator.ToUtc(entry.EndDate); var now = DateTime.UtcNow; if (startUtc <= now && (!endUtc.HasValue || (endUtc.HasValue && endUtc.Value <= now && endUtc.Value > startUtc))) { existingEntry.StartDate = startUtc; existingEntry.EndDate = endUtc; _db.SaveChanges(); } } return(calculator.EnrichTEntry(null, existingEntry)); } } } return(null); } catch (Exception ex) { throw new WebFaultException <string>(ex.Message, HttpStatusCode.BadRequest); } }
public TEntryDto EnrichTEntry(TEntryDto entryDto, TEntry entry) { var dto = entryDto; if (dto == null) { if (entry == null) { return(new TEntryDto { Contrib = 0, EndDate = DateTime.UtcNow, StartDate = DateTime.UtcNow, InProgress = false, IsDeleted = true, ProjectName = "", TaskName = "", TEntryID = 0, TTaskID = 0 }); } dto = Mapper.Map <TEntryDto>(entry); } else { Mapper.Map <TEntry, TEntryDto>(entry, entryDto); } dto.StartDate = ToLocal(entry.StartDate).Value; dto.EndDate = ToLocal(entry.EndDate); if (string.IsNullOrWhiteSpace(dto.TaskName)) { dto.TaskName = entry.TTask.Name; } if (string.IsNullOrWhiteSpace(dto.ProjectName)) { dto.ProjectName = entry.TTask.TProject.Name; } if (dto.Contrib == 0) { dto.Contrib = (DateOrLocalNow(dto.EndDate) - dto.StartDate).TotalSeconds; } return(dto); }