예제 #1
0
        /// <summary>
        /// Deletes a hairStyle by ID
        /// </summary>
        /// <param name="id">ID of the hairStyle to be deleted</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if the hair style with the corresponding <seealso cref="id"/> is not found
        /// </exception>
        /// <returns>HairStyle deleted</returns>
        public async Task <HairStyles> Delete(ulong id)
        {
            HairStyles hairStyle = null;

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

                if (hairStyle != null)
                {
                    _context.HairStyles.Remove(hairStyle);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new ResourceNotFoundException("HairStyle not found");
                }
            }
            else
            {
                hairStyle = HairStyles.Where(u => u.Id == id).FirstOrDefault();

                if (hairStyle != null)
                {
                    HairStyles.Remove(hairStyle);
                }
                else
                {
                    throw new ResourceNotFoundException("HairStyle not found");
                }
            }

            return(hairStyle);
        }
        /// <summary>
        /// Deletes a face shape by ID
        /// </summary>
        /// <param name="id">ID of the face shape to be deleted</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if the face shape with the corresponding <seealso cref="id"/> is not found
        /// </exception>
        /// <returns>Face shape deleted</returns>
        public async Task <FaceShapes> Delete(ulong id)
        {
            FaceShapes faceShape = null;

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

                if (faceShape != null)
                {
                    _context.FaceShapes.Remove(faceShape);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }
            else
            {
                faceShape = FaceShapes.Where(u => u.Id == id).FirstOrDefault();

                if (faceShape != null)
                {
                    FaceShapes.Remove(faceShape);
                }
                else
                {
                    throw new ResourceNotFoundException("Face shape not found");
                }
            }

            return(faceShape);
        }
예제 #3
0
        /// <summary>
        /// Deletes a colour by ID
        /// </summary>
        /// <param name="id">ID of the colour to be deleted</param>
        /// <exception cref="ResourceNotFoundException">
        ///     Thrown if the user with the corresponding <seealso cref="id"/> is not found
        /// </exception>
        /// <returns>Colour deleted</returns>
        public async Task <Colours> Delete(ulong id)
        {
            Colours colour = null;

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

                if (colour != null)
                {
                    _context.Colours.Remove(colour);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    throw new ResourceNotFoundException("Colour not found");
                }
            }
            else
            {
                colour = Colours.Where(u => u.Id == id).FirstOrDefault();

                if (colour != null)
                {
                    Colours.Remove(colour);
                }
                else
                {
                    throw new ResourceNotFoundException("Colour not found");
                }
            }

            return(colour);
        }
        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));
            }
        }
        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));
            }
        }
예제 #6
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));
            }
        }
예제 #7
0
        /// <summary>
        /// Adds a new user
        /// </summary>
        /// <param name="user">User to be added</param>
        /// <returns>User added</returns>
        public async Task <Users> Add(SignUpUser user)
        {
            Users userAdded = null;

            if (_context != null)
            {
                var existingUser = await _context
                                   .Users
                                   .AnyAsync(
                    u => u.UserName == user.UserName ||
                    u.UserEmail == user.UserEmail
                    );

                if (!existingUser)
                {
                    string salt = _authenticationService.GenerateSalt();
                    string hash = _authenticationService.HashPassword(user.UserPassword, salt);

                    userAdded = new Users
                    {
                        UserName         = user.UserName,
                        UserEmail        = user.UserEmail,
                        FirstName        = user.FirstName,
                        LastName         = user.LastName ?? "",
                        UserRole         = user.UserRole,
                        UserPasswordHash = hash,
                        UserPasswordSalt = salt
                    };

                    _context.Users.Add(userAdded);

                    await _context.SaveChangesAsync();

                    userAdded = await _context.Users.FirstOrDefaultAsync(
                        u => u.UserName == userAdded.UserName ||
                        u.UserEmail == userAdded.UserEmail
                        );
                }
            }
            else
            {
                var existingUser = Users.Any(
                    u => u.UserName == user.UserName ||
                    u.UserEmail == user.UserEmail
                    );

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

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

                    userAdded = new Users
                    {
                        UserName         = user.UserName,
                        UserEmail        = user.UserEmail,
                        FirstName        = user.FirstName,
                        LastName         = user.LastName ?? "",
                        UserRole         = user.UserRole,
                        UserPasswordHash = hash,
                        UserPasswordSalt = salt
                    };

                    Users.Add(userAdded);
                }
            }

            return(userAdded);
        }
예제 #8
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));
            }
        }