public async Task <IActionResult> PutColours(ulong id, [FromBody] Colours colours)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

            Colours currentColour = await _context.Colours.FindAsync(id);

            try
            {
                if (currentColour != null)
                {
                    currentColour.ColourName            = colours.ColourName;
                    currentColour.ColourHash            = colours.ColourHash;
                    _context.Entry(currentColour).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }
        /// <summary>
        /// Updates an existing face shape
        /// </summary>
        /// <param name="id">ID of the face shape to be updated</param>
        /// <param name="faceShape">Updated face shape object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="faceShape"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, FaceShapes faceShape)
        {
            bool faceShapeUpdated = false;

            if (_context != null)
            {
                FaceShapes currentFaceShape = await _context.FaceShapes.FindAsync(id);

                try
                {
                    if (currentFaceShape != null)
                    {
                        currentFaceShape.ShapeName             = faceShape.ShapeName;
                        _context.Entry(currentFaceShape).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        faceShapeUpdated = true;
                    }
                    {
                        throw new ResourceNotFoundException("Face shape not found");
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                FaceShapes currentFaceShape = FaceShapes.FirstOrDefault(fs => fs.Id == id);

                if (currentFaceShape != null)
                {
                    int currentFSIndex = FaceShapes.FindIndex(fs => fs.Id == id);
                    FaceShapes[currentFSIndex] = faceShape;
                    faceShapeUpdated           = true;
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }

            return(faceShapeUpdated);
        }
예제 #3
0
        /// <summary>
        /// Updates an existing colour
        /// </summary>
        /// <param name="id">ID of the colour to be updated</param>
        /// <param name="updatedColour">Updated colour object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="updatedColour"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, Colours updatedColour)
        {
            bool colourUpdated = false;

            if (_context != null)
            {
                Colours currentColour = await _context.Colours.FindAsync(id);

                try
                {
                    if (currentColour != null)
                    {
                        currentColour.ColourName            = updatedColour.ColourName;
                        currentColour.ColourHash            = updatedColour.ColourHash;
                        _context.Entry(currentColour).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        colourUpdated = true;
                    }

                    throw new ResourceNotFoundException("Colour not found");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                Colours currentFaceShape = Colours.FirstOrDefault(fs => fs.Id == id);

                if (currentFaceShape != null)
                {
                    int currentColourIndex = Colours.FindIndex(c => c.Id == id);
                    Colours[currentColourIndex] = updatedColour;
                    colourUpdated = true;
                }
                else
                {
                    throw new ResourceNotFoundException("Colour not found");
                }
            }

            return(colourUpdated);
        }
예제 #4
0
        /// <summary>
        /// Updates an existing hairLengthLink
        /// </summary>
        /// <param name="id">ID of the hairLengthLink to be updated</param>
        /// <param name="updatedHairLengthLink">Updated hairLengthLink object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="updatedHairLengthLink"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, HairLengthLinks updatedHairLengthLink)
        {
            bool hairLengthLinkUpdated = false;

            if (_context != null)
            {
                HairLengthLinks currentHairLengthLink = await _context.HairLengthLinks.FindAsync(id);

                try
                {
                    if (currentHairLengthLink != null)
                    {
                        currentHairLengthLink.LinkName = updatedHairLengthLink.LinkName;
                        currentHairLengthLink.LinkUrl  = updatedHairLengthLink.LinkUrl;
                        _context.Entry(currentHairLengthLink).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        hairLengthLinkUpdated = true;
                    }

                    throw new ResourceNotFoundException("HairLengthLink not found");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                HairLengthLinks currentHairLengthLink = HairLengthLinks.FirstOrDefault(fs => fs.Id == id);

                if (currentHairLengthLink != null)
                {
                    int currentHairLengthLinkIndex = HairLengthLinks.FindIndex(c => c.Id == id);
                    HairLengthLinks[currentHairLengthLinkIndex] = updatedHairLengthLink;
                    hairLengthLinkUpdated = true;
                }
                else
                {
                    throw new ResourceNotFoundException("HairLengthLink not found");
                }
            }

            return(hairLengthLinkUpdated);
        }
예제 #5
0
        /// <summary>
        /// Updates an existing hairStyle
        /// </summary>
        /// <param name="id">ID of the hairStyle to be updated</param>
        /// <param name="updatedHairStyle">Updated hairStyle object</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if <seealso cref="updatedHairStyle"/> does not exist
        /// </exception>
        /// <exception cref="DbUpdateConcurrencyException">
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, HairStyles updatedHairStyle)
        {
            bool hairStyleUpdated = false;

            if (_context != null)
            {
                HairStyles currentHairStyle = await _context.HairStyles.FindAsync(id);

                try
                {
                    if (currentHairStyle != null)
                    {
                        currentHairStyle.HairStyleName         = updatedHairStyle.HairStyleName;
                        _context.Entry(currentHairStyle).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        hairStyleUpdated = true;
                    }

                    throw new ResourceNotFoundException("HairStyle not found");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    throw ex;
                }
            }
            else
            {
                HairStyles currentHairStyle = HairStyles.FirstOrDefault(hl => hl.Id == id);

                if (currentHairStyle != null)
                {
                    int currentHairStyleIndex = HairStyles.FindIndex(c => c.Id == id);
                    HairStyles[currentHairStyleIndex] = updatedHairStyle;
                    hairStyleUpdated = true;
                }
                else
                {
                    throw new ResourceNotFoundException("HairStyle not found");
                }
            }

            return(hairStyleUpdated);
        }
        public async Task <IActionResult> PutHairLengthLinks(ulong id, [FromBody] HairLengthLinks hairLengthLinks)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

            var correspondingHairLength = await _context.HairLengths.FirstOrDefaultAsync(h => h.Id == hairLengthLinks.HairLengthId);

            if (correspondingHairLength == null)
            {
                return(NotFound(new { errors = new { HairLengthId = new string[] { "No matching hair length entry was found" } }, status = 404 }));
            }

            HairLengthLinks currentHLL = await _context.HairLengthLinks.FindAsync(id);



            try
            {
                if (currentHLL != null)
                {
                    currentHLL.LinkName              = hairLengthLinks.LinkName;
                    currentHLL.LinkUrl               = hairLengthLinks.LinkUrl;
                    currentHLL.HairLengthId          = hairLengthLinks.HairLengthId;
                    _context.Entry(currentHLL).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }
예제 #7
0
        public async Task <IActionResult> PutFaceShapeLinks(ulong id, [FromBody] FaceShapeLinks faceShapeLinks)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

            var correspondingFaceShape = await _context.FaceShapes.FirstOrDefaultAsync(f => f.Id == faceShapeLinks.FaceShapeId);

            if (correspondingFaceShape == null)
            {
                return(NotFound(new { errors = new { FaceShapeId = new string[] { "No matching face shape entry was found" } }, status = 404 }));
            }

            FaceShapeLinks fsl = await _context.FaceShapeLinks.FindAsync(id);

            try
            {
                if (fsl != null)
                {
                    fsl.LinkName    = faceShapeLinks.LinkName;
                    fsl.LinkUrl     = faceShapeLinks.LinkUrl;
                    fsl.FaceShapeId = faceShapeLinks.FaceShapeId;

                    _context.Entry(fsl).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }
예제 #8
0
        /// <summary>
        /// Updates an existing user
        /// </summary>
        /// <param name="id">ID of the user to be updated</param>
        /// <param name="user">Updated user object</param>
        /// <exception cref="ExistingUserException">
        ///     Thrown if another user has the same
        ///     <see cref="Users.UserName"/> or
        ///     <see cref="Users.UserEmail"/>
        ///     as <seealso cref="user"/>
        /// </exception>
        /// <returns>Result of the operation</returns>
        public async Task <bool> Edit(ulong id, UpdatedUser user)
        {
            bool userUpdated       = false;
            bool existingUserName  = false;
            bool existingUserEmail = false;

            if (_context != null)
            {
                existingUserName = await _context.Users.AnyAsync(u => u.Id != user.Id && u.UserName == user.UserName);

                existingUserEmail = await _context.Users.AnyAsync(u => u.Id != user.Id && u.UserEmail == user.UserEmail);

                if (existingUserName)
                {
                    throw new ExistingUserException("Username is taken");
                }

                if (existingUserEmail)
                {
                    throw new ExistingUserException("Email is taken");
                }

                // hash/salt new password
                string salt = _authenticationService.GenerateSalt();
                string hash = _authenticationService.HashPassword(user.UserPassword, salt);

                var currentUser = await _context.Users.FindAsync(user.Id);

                try
                {
                    if (currentUser != null)
                    {
                        currentUser.UserName              = user.UserName;
                        currentUser.UserPasswordHash      = hash;
                        currentUser.UserPasswordSalt      = salt;
                        currentUser.FirstName             = user.FirstName;
                        currentUser.LastName              = user.LastName ?? currentUser?.LastName;
                        currentUser.UserEmail             = user.UserEmail;
                        currentUser.UserRole              = user.UserRole ?? currentUser?.UserRole;
                        currentUser.DateCreated           = currentUser?.DateCreated;
                        _context.Entry(currentUser).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        userUpdated = true;
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(false);
                }
            }
            else
            {
                existingUserName  = Users.Any(u => u.Id != user.Id && u.UserName == user.UserName);
                existingUserEmail = Users.Any(u => u.Id != user.Id && u.UserEmail == user.UserEmail);

                if (existingUserName)
                {
                    throw new ExistingUserException("Username is taken");
                }

                if (existingUserEmail)
                {
                    throw new ExistingUserException("Email is taken");
                }

                var authenticationService = new AuthenticationService(
                    new AppSettings {
                    Secret = "Secret1", Pepper = "Pepper1"
                },
                    this
                    );

                // hash/salt new password
                string salt = authenticationService.GenerateSalt();
                string hash = authenticationService.HashPassword(user.UserPassword, salt);

                var currentUser = Users.FirstOrDefault(u => u.Id == user.Id);

                if (currentUser != null)
                {
                    currentUser.UserName         = user.UserName;
                    currentUser.UserPasswordHash = hash;
                    currentUser.UserPasswordSalt = salt;
                    currentUser.FirstName        = user.FirstName;
                    currentUser.LastName         = user.LastName ?? currentUser?.LastName;
                    currentUser.UserEmail        = user.UserEmail;
                    currentUser.UserRole         = user.UserRole ?? currentUser?.UserRole;
                    currentUser.DateCreated      = currentUser?.DateCreated;
                    userUpdated = true;
                }
            }

            return(userUpdated);
        }
예제 #9
0
        public async Task <IActionResult> PutUsers(ulong id, [FromBody] UpdatedUser user)
        {
            if (!_authorizationService.ValidateJWTToken(Request))
            {
                return(Unauthorized(new { errors = new { Token = new string[] { "Invalid token" } }, status = 401 }));
            }

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

            var existingUserName = await _context.Users.AnyAsync(u => u.Id != user.Id && u.UserName == user.UserName);

            if (existingUserName)
            {
                return(Conflict(new { errors = new { UserName = new string[] { "Username is already taken" } }, status = 409 }));
            }

            var existingEmail = await _context.Users.AnyAsync(u => u.Id != user.Id && u.UserEmail == user.UserEmail);

            if (existingEmail)
            {
                return(Conflict(new { errors = new { UserEmail = new string[] { "Email is already registered" } }, status = 409 }));
            }

            // hash/salt new password
            string salt = _authenticationService.GenerateSalt();
            string hash = _authenticationService.HashPassword(user.UserPassword, salt);

            Users currentUser = await _context.Users.FindAsync(user.Id);

            try
            {
                if (currentUser != null)
                {
                    currentUser.UserName              = user.UserName;
                    currentUser.UserPasswordHash      = hash;
                    currentUser.UserPasswordSalt      = salt;
                    currentUser.FirstName             = user.FirstName;
                    currentUser.LastName              = user.LastName ?? currentUser?.LastName;
                    currentUser.UserEmail             = user.UserEmail;
                    currentUser.UserRole              = user.UserRole ?? currentUser?.UserRole;
                    currentUser.DateCreated           = currentUser?.DateCreated;
                    _context.Entry(currentUser).State = EntityState.Modified;
                    await _context.SaveChangesAsync();

                    // Authenticate user
                    var authenticatedUser = await _authenticationService.Authenticate(user.UserEmail, user.UserPassword);

                    if (authenticatedUser == null)
                    {
                        // User isn't registered
                        Response.Headers.Append("Access-Control-Allow-Origin", Request.Headers["Origin"]);
                        return(Unauthorized(new { errors = new { Authentication = new string[] { "Invalid username, email and/or password" } }, status = 401 }));
                    }

                    // Return 200 OK with token in cookie
                    var existingUser = await _context.Users.Where(u => u.Id == authenticatedUser.Id).FirstOrDefaultAsync();

                    authenticatedUser.BaseUser = existingUser;

                    _authorizationService.SetAuthCookie(Request, Response, authenticatedUser.Token);
                    Response.Headers.Append("X-Authorization-Token", authenticatedUser.Token);

                    return(Ok(existingUser.WithoutPassword()));
                }
                return(NotFound());
            }
            catch (DbUpdateConcurrencyException)
            {
                return(StatusCode(500));
            }
        }