public async Task <ApiResult <List <TimeEntryReadEditDto> > > UpdateEntry(TimeEntryReadEditDto dto)
        {
            try
            {
                var entryToUpdate = await _context.TimeEntries.FirstOrDefaultAsync(t => t.Id == dto.Id);

                entryToUpdate.MemberId   = dto.MemberId;
                entryToUpdate.PositionId = dto.PositionId;
                entryToUpdate.StartTime  = dto.StartTime;
                entryToUpdate.EndTime    = dto.EndTime;

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("An error occured when trying to update a time entry:\n\n" + ex.Message);

                return(new ApiResult <List <TimeEntryReadEditDto> >
                {
                    Successful = false,
                    Error = "Something went wrong when trying to update the time entry. Please try to reload the page."
                });
            }

            if (dto.EndTime == null)
            {
                return(await GetCurrentTimeEntries());
            }
            else
            {
                return(await GetArchivedTimeEntries());
            }
        }
Beispiel #2
0
        public void UpdateTimeEntry_Success()
        {
            using (var transaction = _db.Connection.BeginTransaction())
            {
                var context   = _db.CreateContext(transaction);
                var tsService = new EFTimeSheetService(context, _mapper, new NullLogger <EFTimeSheetService>());

                int idOfUpdatedEntry = context.TimeEntries.FirstOrDefault().Id;

                var newStartTime  = DateTime.Now;
                var newEndTime    = DateTime.Now + new TimeSpan(3, 0, 0);
                var newMemberId   = context.Members.FirstOrDefault().Id + 1;
                var newPositionId = context.Positions.FirstOrDefault().Id + 1;

                var dto = new TimeEntryReadEditDto
                {
                    Id         = idOfUpdatedEntry,
                    MemberId   = newMemberId,
                    PositionId = newPositionId,
                    StartTime  = newStartTime,
                    EndTime    = newEndTime
                };

                var result = tsService.UpdateEntry(dto).Result;
                Assert.True(result.Successful);
                Assert.True(context.TimeEntries.FirstOrDefault().StartTime == newStartTime);
                Assert.True(context.TimeEntries.FirstOrDefault().EndTime == newEndTime);
                Assert.True(context.TimeEntries.FirstOrDefault().MemberId == newMemberId);
                Assert.True(context.TimeEntries.FirstOrDefault().PositionId == newPositionId);
            }
        }
 public async Task <IActionResult> UpdateEntry(TimeEntryReadEditDto dto)
 {
     return(new ObjectResult(await _tsService.UpdateEntry(dto)));
 }
Beispiel #4
0
        public async Task <ApiResult <List <TimeEntryReadEditDto> > > UpdateTimeEntry(TimeEntryReadEditDto timeEntry)
        {
            try
            {
                var response = await _http.PostAsJsonAsync($"{Constants.ControllerNames.Timesheet}/{Constants.Routes.Update}", timeEntry);

                return(response.Content.ReadFromJsonAsync <ApiResult <List <TimeEntryReadEditDto> > >().Result);
            }
            catch (Exception ex)
            {
                return(new ApiResult <List <TimeEntryReadEditDto> >
                {
                    Successful = false,
                    Error = $"Something went wrong when trying to update a time entry:\n\n{ex.Message}\n{ex.StackTrace}"
                });
            }
        }