示例#1
0
        public async Task <IHttpActionResult> Put(UpsertTimeEntryCommand command)
        {
            try
            {
                var upserted = await timeEntryService.UpsertTimeEntry(command);

                return(Ok());
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AuthorizationException)
            {
                return(StatusCode(System.Net.HttpStatusCode.Forbidden));
            }
        }
        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);
        }
示例#3
0
        public async Task <IHttpActionResult> Put([FromUri] Guid id, [FromBody] UpsertTimeEntryCommand command)
        {
            try
            {
                if (command != null)
                {
                    command.Id = id;
                }

                var upserted = await timeEntryService.UpsertTimeEntry(command);

                return(Ok());
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (AuthorizationException)
            {
                return(StatusCode(System.Net.HttpStatusCode.Forbidden));
            }
        }