示例#1
0
        public async Task <IActionResult> RemoveFeatureForUser([FromBody] UserFeatures userFeature)
        {
            UserFeatureMapping mapping = await _context.UserFeatureMappings.FirstOrDefaultAsync(x => x.UserId == userFeature.UserId && x.FeatureId == userFeature.FeatureId);

            if (mapping != null)
            {
                _context.UserFeatureMappings.Remove(mapping);
            }

            await _context.SaveChangesAsync();

            return(this.Ok());
        }
示例#2
0
        public async Task <IActionResult> AddFeatureToUser([FromBody] UserFeatures userFeature)
        {
            UserFeatureMapping mapping = new UserFeatureMapping()
            {
                FeatureId = userFeature.FeatureId,
                UserId    = userFeature.UserId
            };

            _context.Add(mapping);
            await _context.SaveChangesAsync();

            return(this.Ok());
        }
        public async Task <IActionResult> PutUserFeatures(ulong id, [FromBody] UserFeatures userFeatures)
        {
            if (!_authorizationService.ValidateJWTCookie(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

            if (id != userFeatures.Id)
            {
                return(BadRequest(new { errors = new { Id = new string[] { "ID sent does not match the one in the endpoint" } }, status = 400 }));
            }

            // Check existing resources
            var errors = await CheckExistingResources(userFeatures);

            if (errors != null)
            {
                return(NotFound(errors));
            }

            _context.Entry(userFeatures).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserFeaturesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        private async Task <object> CheckExistingResources(UserFeatures userFeatures)
        {
            // Check existing user
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.USERS))
            {
                return(new { errors = new { UserId = new string[] { "No matching user entry was found" } }, status = 404 });
            }

            // Check existing face shape
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.FACE_SHAPES))
            {
                return(new { errors = new { FaceShapeId = new string[] { "No matching face shape entry was found" } }, status = 404 });
            }

            // Check existing skin tone
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.SKIN_TONES))
            {
                return(new { errors = new { SkinToneId = new string[] { "No matching skin tone entry was found" } }, status = 404 });
            }

            // Check existing hair style
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.HAIR_STYLES))
            {
                return(new { errors = new { HairStyleId = new string[] { "No matching hair style entry was found" } }, status = 404 });
            }

            // Check existing hair length
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.HAIR_LENGTHS))
            {
                return(new { errors = new { HairLengthId = new string[] { "No matching hair length entry was found" } }, status = 404 });
            }

            // Check existing hair colour
            if (!await CorrespondingResourceExists(userFeatures, ResourceTypes.COLOURS))
            {
                return(new { errors = new { HairColourId = new string[] { "No matching hair colour entry was found" } }, status = 404 });
            }

            return(null);
        }