// creates an new application and returns the object as a DTO public ApplicationDetailDto CreateApplication(ApplicationCreateDto applicationToCreate) { var newApplication = applicationToCreate.ToModel(); newApplication.IsCurrent = true; newApplication.Version = 1; _applicationDbContext.Application.Add(newApplication); Save(); return(GetApplicationById(newApplication.Id)); }
// updates an Application // the updated application is set to IsCurrent = false // a new application will be created referencing the old one public ApplicationDetailDto UpdateApplication(Guid applicationId, ApplicationCreateDto newApplication) { var currentApplication = _applicationDbContext.Application .Include(application => application.Comment) .Include(application => application.Assignment) .SingleOrDefault(app => app.Id == applicationId); currentApplication.IsCurrent = false; var updatedApplication = newApplication.ToModel(); updatedApplication.IsCurrent = true; updatedApplication.Version = currentApplication.Version + 1; updatedApplication.PreviousVersion = currentApplication.Id; _applicationDbContext.Application.Add(updatedApplication); // Copies the old comments to the new application currentApplication.Comment?.ToList() .ForEach(comment => { var copyOfComment = new Comment() { Id = Guid.NewGuid(), ApplicationId = updatedApplication.Id, Created = DateTime.UtcNow, IsPrivate = comment.IsPrivate, RequiresChanges = comment.RequiresChanges, Message = comment.Message, UserId = comment.UserId }; _applicationDbContext.Comment.Add(copyOfComment); }); // copies the old assignments to the new one currentApplication.Assignment?.ToList().ForEach(assignment => { _applicationDbContext.Assignment.Add(new Assignment() { ApplicationId = updatedApplication.Id, UserId = assignment.UserId }); }); Save(); return(GetApplicationById(updatedApplication.Id)); }