async Task <TimeEntry> ITimeEntryService.UpsertTimeEntry(UpsertTimeEntryCommand command) { var principal = await currentUserResolver.ResolveCurrentClaimsPrincipalAsync(); await authorizationService.AuthorizeResourceType(principal, Operation.Upsert, typeof(TimeEntry)); Check.NotNull(command, errorMessage: "Command can not be null."); await validationService.Validate(command); var timeEntryEntity = await timeEntryRepository.GetTimeEntryById(command.Id); if (timeEntryEntity != null) { await authorizationService.AuthorizeResource(principal, Operation.Update, timeEntryEntity); timeEntryEntity.Duration = command.Duration; timeEntryEntity.Note = command.Note; timeEntryEntity.Date = command.Date; if (timeEntryEntity.OwnerId != command.OwnerId) { // If owner ID is changed, check if current principal has permission to do so timeEntryEntity.OwnerId = command.OwnerId; await authorizationService.AuthorizeResource(principal, Operation.Update, timeEntryEntity); } await timeEntryRepository.UpdateTimeEntry(timeEntryEntity); } else { timeEntryEntity = new TimeEntry() { Id = command.Id, Date = command.Date, Duration = command.Duration, Note = command.Note, OwnerId = command.OwnerId }; await authorizationService.AuthorizeResource(principal, Operation.Create, timeEntryEntity); await timeEntryRepository.CreateTimeEntry(timeEntryEntity); } return(timeEntryEntity); }