예제 #1
0
        /// <summary>
        /// Updates a dashboard option using a <see cref="Delta"/> object.
        /// </summary>
        /// <param name="id">ID of the entity to be updated.</param>
        /// <param name="delta">
        /// Delta containing a list of entity properties.  Web Api does the magic of converting the JSON to
        /// a delta.
        /// </param>
        /// <returns>
        /// An asynchronous task result containing information needed to create an API response message.
        /// </returns>
        public override async Task <CommandResult <DashboardOptionBaseDto, Guid> > Update(Guid id, Delta <DashboardOptionBaseDto> delta)
        {
            var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal();

            if (userId == null)
            {
                return(Command.Error <DashboardOptionBaseDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            if (delta == null)
            {
                return(Command.Error <DashboardOptionBaseDto>(EntityErrorCode.EntityFormatIsInvalid));
            }

            var userDashboardOptions = _context.GetDashboardOptionsForUser(Guid.Parse(userId));
            var entity = userDashboardOptions.SingleOrDefault(x => x.Id == id);

            if (entity == null)
            {
                return(Command.Error <DashboardOptionBaseDto>(EntityErrorCode.EntityNotFound));
            }

            var dto = _mapper.Map(entity, new DashboardOptionBaseDto());

            delta.Patch(dto);

            var validationResponse = ValidatorUpdate.Validate(dto);

            var userTenants = _context.GetTenantsForUser(Guid.Parse(userId));

            if (dto.TenantId != Guid.Empty && !userTenants.Any(x => x.Id == dto.TenantId))
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId"));
            }
            else if (_context.DashboardOptions.Any(x => x.Id != id && x.TenantId == dto.TenantId))
            {
                validationResponse.FFErrors.Add(EntityErrorCode.EntityAlreadyExists);
            }

            // Including the original Id in the Patch request will not return an error but attempting to change the Id is not allowed.
            if (dto.Id != id)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityIDUpdateNotAllowed("Id"));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <DashboardOptionBaseDto>(validationResponse));
            }

            _context.DashboardOptions.Attach(entity);
            _mapper.Map(dto, entity);
            entity.SetAuditFieldsOnUpdate(userId);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.NoContent <DashboardOptionBaseDto>());
        }
예제 #2
0
        /// <summary>
        /// Updates a Location Log Entry using a <see cref="Delta"/> object.
        /// </summary>
        /// <param name="id">ID of the Location Log Entry to be updated.</param>
        /// <param name="delta">
        /// Delta containing a list of Location Log Entry properties.  Web Api does the magic of converting the JSON to
        /// a delta.
        /// </param>
        /// <returns>
        /// An asynchronous task result containing information needed to create an API response message.
        /// </returns>
        public override async Task <CommandResult <LocationLogEntryBaseDto, Guid> > Update(Guid id, Delta <LocationLogEntryBaseDto> delta)
        {
            // User ID should always be available, but if not ...
            var userId = GetCurrentUser();

            if (!userId.HasValue)
            {
                return(Command.Error <LocationLogEntryBaseDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            if (delta == null)
            {
                return(Command.Error <LocationLogEntryBaseDto>(EntityErrorCode.EntityFormatIsInvalid));
            }

            var locationLogEntry = await _context.GetLocationLogEntriesForUser(userId.Value)
                                   .SingleOrDefaultAsync(l => l.Id == id)
                                   .ConfigureAwait(false);

            if (locationLogEntry == null)
            {
                return(Command.Error <LocationLogEntryBaseDto>(EntityErrorCode.EntityNotFound));
            }

            var dto = _mapper.Map(locationLogEntry, new LocationLogEntryQueryDto());

            delta.Patch(dto);

            var validationResponse = ValidatorUpdate.Validate(dto);

            // Including the original ID in the Patch request will not return an error but attempting to change the Id is not allowed.
            if (dto.Id != id)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityIDUpdateNotAllowed("Id"));
            }

            var locationExists = await DoesLocationExist(dto.LocationId);

            if (!locationExists)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist(nameof(LocationLogEntry.LocationId)));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <LocationLogEntryBaseDto>(validationResponse));
            }

            _context.LocationLogEntries.Attach(locationLogEntry);
            _mapper.Map(dto, locationLogEntry);

            locationLogEntry.SetAuditFieldsOnUpdate(userId.Value);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.NoContent <LocationLogEntryBaseDto>());
        }
예제 #3
0
        /// <summary>
        /// Updates a limit type using a <see cref="Delta"/> object.
        /// </summary>
        /// <param name="id">ID of the entity to be updated.</param>
        /// <param name="delta">
        /// Delta containing a list of entity properties.  Web Api does the magic of converting the JSON to
        /// a delta.
        /// </param>
        /// <returns>
        /// An asynchronous task result containing information needed to create an API response message.
        /// </returns>
        public override async Task <CommandResult <LimitTypeBaseDto, Guid> > Update(Guid id, Delta <LimitTypeBaseDto> delta)
        {
            var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal();

            if (userId == null)
            {
                return(Command.Error <LimitTypeBaseDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            if (delta == null)
            {
                return(Command.Error <LimitTypeBaseDto>(EntityErrorCode.EntityFormatIsInvalid));
            }

            var entity = await _context.LimitTypes
                         .SingleOrDefaultAsync(x => x.Id == id)
                         .ConfigureAwait(false);

            if (entity == null)
            {
                return(Command.Error <LimitTypeBaseDto>(EntityErrorCode.EntityNotFound));
            }

            var dto = _mapper.Map(entity, new LimitTypeBaseDto());

            delta.Patch(dto);

            var validationResponse = ValidatorUpdate.Validate(dto);

            if (_context.LimitTypes.Any(x => x.Id != id && x.I18NKeyName == dto.I18NKeyName))
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityPropertyDuplicateNotAllowed(nameof(LocationType.I18NKeyName)));
            }

            // Including the original Id in the Patch request will not return an error but attempting to change the Id is not allowed.
            if (dto.Id != id)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityIDUpdateNotAllowed("Id"));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <LimitTypeBaseDto>(validationResponse));
            }

            _context.LimitTypes.Attach(entity);
            _mapper.Map(dto, entity);
            entity.SetAuditFieldsOnUpdate(userId);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.NoContent <LimitTypeBaseDto>());
        }
예제 #4
0
        /// <summary>
        /// Updates a location using a <see cref="Delta"/> object.
        /// </summary>
        /// <param name="id">ID of the location to be updated.</param>
        /// <param name="delta">
        /// Delta containing a list of location properties.  Web Api does the magic of converting the JSON to
        /// a delta.
        /// </param>
        /// <returns>
        /// An asynchronous task result containing information needed to create an API response message.
        /// </returns>
        public override async Task <CommandResult <LocationBaseDto, Guid> > Update(Guid id, Delta <LocationBaseDto> delta)
        {
            // Thread.CurrentPrincipal is not available in the constrtor.  Do not try and move this
            var uid = GetCurrentUser();

            // User ID should always be available, but if not ...
            if (!uid.HasValue)
            {
                return(Command.Error <LocationBaseDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            if (delta == null)
            {
                return(Command.Error <LocationBaseDto>(EntityErrorCode.EntityFormatIsInvalid));
            }

            var location = await _context.GetLocationsForUser(uid.Value)
                           .SingleOrDefaultAsync(l => l.Id == id)
                           .ConfigureAwait(false);

            if (location == null)
            {
                return(Command.Error <LocationBaseDto>(EntityErrorCode.EntityNotFound));
            }

            var locationDto = _mapper.Map(location, new LocationBaseDto());

            delta.Patch(locationDto);

            var validationResponse = ValidatorUpdate.Validate(locationDto);

            if (locationDto.ParentId.HasValue)
            {
                var existingTask = _context.Locations.AnyAsync(l => l.Id == locationDto.ParentId.Value);

                if (!existingTask.Result)
                {
                    validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist(nameof(Location.ParentId)));
                }
                else
                {
                    // Check for circular references
                    if (await LocationValidator.IsCircularReference(_context.Locations, locationDto, id))
                    {
                        validationResponse.FFErrors.Add(ValidationErrorCode.CircularReferenceNotAllowed(nameof(Location.ParentId)));
                    }
                }
            }

            // Check that Location Type exists
            if (locationDto.LocationTypeId != Guid.Empty && !_context.LocationTypes.Any(lt => lt.Id == locationDto.LocationTypeId))
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist(nameof(Location.LocationTypeId)));
            }

            // Including the original Id in the Patch request will not return an error but attempting to change the Id is not allowed.
            if (locationDto.Id != id)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityIDUpdateNotAllowed("Id"));
            }

            // Check that unique fields are still unique
            if (_context.Locations.Any(l => l.Id != id && l.Name == locationDto.Name))
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityPropertyDuplicateNotAllowed(nameof(Location.Name)));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <LocationBaseDto>(validationResponse));
            }

            _context.Locations.Attach(location);
            _mapper.Map(locationDto, location);

            location.SetAuditFieldsOnUpdate(uid.Value);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.NoContent <LocationBaseDto>());
        }
예제 #5
0
        public override async Task <CommandResult <InAppMessageBaseDto, Guid> > Update(Guid id, Delta <InAppMessageBaseDto> delta)
        {
            // Check user has proper access to update the message
            var uid = GetCurrentUser();

            if (!uid.HasValue)
            {
                return(Command.Error <InAppMessageBaseDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            if (delta == null)
            {
                return(Command.Error <InAppMessageBaseDto>(EntityErrorCode.EntityFormatIsInvalid));
            }

            var entity = _context.InAppMessages.SingleOrDefault(msg => msg.Id == id);

            if (entity == null)
            {
                return(Command.Error <InAppMessageBaseDto>(EntityErrorCode.EntityNotFound));
            }

            // Check if the calling User shares a Tenant with the UserId passed in
            var tenants     = _context.GetTenantsForUser(uid.Value);
            var shareTenant = tenants.Any(t => t.Users.Any(u => u.Id == entity.UserId));

            if (!shareTenant)
            {
                return(Command.Error <InAppMessageBaseDto>(ValidationErrorCode.ForeignKeyValueDoesNotExist("UserId")));
            }

            var dto = _mapper.Map(entity, new InAppMessageBaseDto());

            delta.Patch(dto);

            var validationResponse = ValidatorUpdate.Validate(dto);

            // Including the original Id in the Patch request will not return an error but attempting to change the Id is not allowed.
            if (dto.Id != id)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.EntityIDUpdateNotAllowed("Id"));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <InAppMessageBaseDto>(validationResponse));
            }

            // Apply the update
            _context.InAppMessages.Attach(entity);

            //if the entity's IsRead flag get set to true from false, then set the DateRead to now
            if (dto.IsRead == true == !entity.IsRead)
            {
                entity.DateRead = DateTime.UtcNow;
            }
            else if (dto.IsRead == false == !entity.IsRead)
            {
                entity.DateRead = null;
            }

            _mapper.Map(dto, entity);
            entity.SetAuditFieldsOnUpdate(uid.Value);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.NoContent <InAppMessageBaseDto>());
        }