public async Task <IActionResult> ViewUpdate(int id, int page = 1) { page = page >= 1 ? page : 1; const int tasksPerPage = 10; int skip = (page - 1) * tasksPerPage; ViewUpdateCaseIOModel outputModel = await casesService.GetCaseByIdAsync(id, skip, tasksPerPage); // If there are no results and need for paging just return model with empty Cases collection and don't do any paging logic if (outputModel.AllTasks == 0) { return(View(outputModel)); } outputModel.LastTasksPage = (int)Math.Ceiling((decimal)outputModel.AllTasks / tasksPerPage); if (page > outputModel.LastTasksPage) { return(RedirectToAction("ViewUpdate", new { id, page = outputModel.LastTasksPage })); } outputModel.CurrentTasksPage = page; return(View(outputModel)); }
public async Task <IActionResult> Update(ViewUpdateCaseIOModel inputModel) { if (!ModelState.IsValid) { return(View("Error", new ErrorViewModel())); } string userId = usersService.UserManager.GetUserId(User); int updateResult = await casesService.UpdateCaseAsync(inputModel, userId); if (updateResult > 0) { await usersService.UpdateUserLastActivityDateAsync(userId); TempData["CaseUpdatedSuccessfully"] = true; } return(RedirectToAction("ViewUpdate", new { id = inputModel.Id })); }
public async Task <ViewUpdateCaseIOModel> GetCaseByIdAsync(int id, int skipTasks, int takeTasks) { ViewUpdateCaseIOModel outputModel = await dbContext.Cases .Select(c => new ViewUpdateCaseIOModel { Id = c.Id, Number = c.Number, CreatedOn = c.CreatedOn, TypeId = c.Type.Id, StatusId = c.Status.Id, PriorityId = c.Priority.Id, PhaseId = c.Phase.Id, ServiceId = c.Service.Id, Subject = c.Subject, Description = c.Description, AllTasks = c.Tasks.Count(), CaseStatuses = dbContext.CaseStatuses.ToArray(), CasePriorities = dbContext.CasePriorities.ToArray(), CaseTypes = dbContext.CaseTypes.ToArray(), CaseServices = dbContext.Services.ToArray(), }) .Where(c => c.Id == id) .FirstOrDefaultAsync(); outputModel.Tasks = await dbContext.Tasks .Where(t => t.CaseId == id) .OrderByDescending(t => t.CreatedOn) .Skip(skipTasks) .Take(takeTasks) .Select(t => new TaskOutputModel { Id = t.Id, CreatedOn = t.CreatedOn, Action = t.Action, NextAction = t.NextAction, Type = t.Type.Type, Status = t.Status.Status, Agent = t.User.Email }).ToArrayAsync(); return(outputModel); }
public async Task <int> UpdateCaseAsync(ViewUpdateCaseIOModel inputModel, string userId) { Case caseRecordToUpdate = await dbContext.Cases .FirstOrDefaultAsync(c => c.Id == inputModel.Id); List <FieldModification> fieldModifications = new List <FieldModification>(); if (caseRecordToUpdate.Number != inputModel.Number) { fieldModifications.Add(new FieldModification { FieldName = "Number", OldValue = caseRecordToUpdate.Number, NewValue = inputModel.Number, }); } if (caseRecordToUpdate.Description != inputModel.Description) { fieldModifications.Add(new FieldModification { FieldName = "Description", OldValue = caseRecordToUpdate.Description, NewValue = inputModel.Description, }); } if (caseRecordToUpdate.Subject != inputModel.Subject) { fieldModifications.Add(new FieldModification { FieldName = "Subject", OldValue = caseRecordToUpdate.Subject, NewValue = inputModel.Subject, }); } if (caseRecordToUpdate.StatusId != inputModel.StatusId) { fieldModifications.Add(new FieldModification { FieldName = "Status", OldValue = await dbContext.CaseStatuses.Where(cs => cs.Id == caseRecordToUpdate.StatusId).Select(cs => cs.Status).FirstOrDefaultAsync(), NewValue = await dbContext.CaseStatuses.Where(cs => cs.Id == inputModel.StatusId).Select(cs => cs.Status).FirstOrDefaultAsync(), }); } if (caseRecordToUpdate.TypeId != inputModel.TypeId) { fieldModifications.Add(new FieldModification { FieldName = "Type", OldValue = await dbContext.CaseTypes.Where(ct => ct.Id == caseRecordToUpdate.TypeId).Select(ct => ct.Type).FirstOrDefaultAsync(), NewValue = await dbContext.CaseTypes.Where(ct => ct.Id == inputModel.TypeId).Select(ct => ct.Type).FirstOrDefaultAsync(), }); } if (caseRecordToUpdate.PriorityId != inputModel.PriorityId) { fieldModifications.Add(new FieldModification { FieldName = "Priority", OldValue = await dbContext.CasePriorities.Where(cp => cp.Id == caseRecordToUpdate.PriorityId).Select(cp => cp.Priority).FirstOrDefaultAsync(), NewValue = await dbContext.CasePriorities.Where(cp => cp.Id == inputModel.PriorityId).Select(cp => cp.Priority).FirstOrDefaultAsync(), }); } if (caseRecordToUpdate.PhaseId != inputModel.PhaseId) { fieldModifications.Add(new FieldModification { FieldName = "Phase", OldValue = await dbContext.CasePhases.Where(cp => cp.Id == caseRecordToUpdate.PhaseId).Select(cp => cp.Phase).FirstOrDefaultAsync(), NewValue = await dbContext.CasePhases.Where(cp => cp.Id == inputModel.PhaseId).Select(cp => cp.Phase).FirstOrDefaultAsync(), }); } if (caseRecordToUpdate.ServiceId != inputModel.ServiceId) { fieldModifications.Add(new FieldModification { FieldName = "Service", OldValue = await dbContext.Services.Where(s => s.Id == caseRecordToUpdate.ServiceId).Select(s => s.ServiceName).FirstOrDefaultAsync(), NewValue = await dbContext.Services.Where(s => s.Id == inputModel.ServiceId).Select(s => s.ServiceName).FirstOrDefaultAsync(), }); } caseRecordToUpdate.Number = inputModel.Number; caseRecordToUpdate.Description = inputModel.Description; caseRecordToUpdate.Subject = inputModel.Subject; caseRecordToUpdate.LastModified = DateTime.UtcNow; caseRecordToUpdate.StatusId = inputModel.StatusId; caseRecordToUpdate.TypeId = inputModel.TypeId; caseRecordToUpdate.PriorityId = inputModel.PriorityId; caseRecordToUpdate.PhaseId = inputModel.PhaseId; caseRecordToUpdate.ServiceId = inputModel.ServiceId; if (fieldModifications.Count > 0) { CaseModificationLogRecord modificationLogRecord = new CaseModificationLogRecord { ModificationTime = DateTime.UtcNow, UserId = userId, CaseId = caseRecordToUpdate.Id, ModifiedFields = fieldModifications, }; dbContext.CaseModificationLogRecords.Add(modificationLogRecord); } dbContext.Cases.Update(caseRecordToUpdate); int saveResult = await dbContext.SaveChangesAsync(); return(saveResult); }