Exemple #1
0
        public LacesResponse UnfollowUser(UserFollowRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User followedUser  = new LacesDataModel.User.User(request.FollowedUserId);
                    LacesDataModel.User.User followingUser = new LacesDataModel.User.User(request.UserId);

                    UserFollow follow = new UserFollow();
                    follow.LoadByUserids(followingUser.UserId, followedUser.UserId);

                    if (follow.UserFollowId > 0 && follow.Delete())
                    {
                        followedUser.UsersFollowing--;
                        followedUser.Update();

                        followingUser.UsersFollowed--;
                        followingUser.Update();

                        response.Success = true;
                        response.Message = "Operation completed successfully";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
        public GetCommentResponse GetComment(CommentRequest request)
        {
            GetCommentResponse response = new GetCommentResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    Comment comment = new Comment(request.CommentId);
                    LacesDataModel.User.User user = new LacesDataModel.User.User(comment.UserId);
                    Image userImage = new Image();

                    userImage.LoadAvatarByUserId(comment.UserId);

                    response.CreatedDate = comment.CreatedDate;
                    response.Text        = comment.Text;
                    response.UpdatedDate = comment.UpdatedDate;

                    response.UserImage = new LacesAPI.Models.Response.ImageInfo();
                    response.UserImage.DateLastChanged = userImage.UpdatedDate;
                    response.UserImage.FileData        = File.ReadAllBytes(userImage.FilePath);
                    response.UserImage.FileFormat      = userImage.FileFormat;
                    response.UserImage.FileName        = userImage.FileName;

                    response.UserName = user.UserName;

                    response.Success = true;
                    response.Message = "Comment details retrieved succesfully.";
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new GetCommentResponse();
                response.Success = false;

                if (ex.Message.Contains("find comment") || ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #3
0
        public LoginUserResponse ValidateLogin(LoginUserRequest request)
        {
            LoginUserResponse response = new LoginUserResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User();
                    user.UserName = request.UserName;
                    user.Password = request.Password;

                    if (user.ValidateLogin())
                    {
                        if (user.UserId > 0)
                        {
                            response.UserId  = user.UserId;
                            response.Success = true;
                            response.Message = "Validation succesful.";
                        }
                        else
                        {
                            response.UserId  = 0;
                            response.Success = false;
                            response.Message = "Invalid credentials";
                        }
                    }
                    else
                    {
                        response         = new LoginUserResponse();
                        response.UserId  = 0;
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch
            {
                response         = new LoginUserResponse();
                response.UserId  = 0;
                response.Success = false;
                response.Message = "An unexpected error has occurred; please verify the format of your request.";
            }


            return(response);
        }
Exemple #4
0
        public LacesResponse LikeProduct(ProductRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    // Confirm user and product exist
                    LacesDataModel.User.User       user    = new LacesDataModel.User.User(request.UserId);
                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId);

                    UserLike like = new UserLike();
                    like.UserId      = user.UserId;
                    like.ProductId   = product.ProductId;
                    like.CreatedDate = DateTime.Now;

                    if (like.Add())
                    {
                        response.Success = true;
                        response.Message = "Operation completed.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new LacesResponse();
                response.Success = false;

                if (ex.Message.Contains("find user") || ex.Message.Contains("find product") || ex.Message.Contains("find like"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
        public LacesResponse AddComment(AddCommentRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    Comment comment = new Comment();

                    // Confirm user and product exist
                    LacesDataModel.User.User       user    = new LacesDataModel.User.User(request.UserId);
                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId);

                    comment.CreatedDate = DateTime.Now;
                    comment.ProductId   = product.ProductId;
                    comment.Text        = request.Text;
                    comment.UpdatedDate = DateTime.Now;
                    comment.UserId      = user.UserId;

                    if (comment.Add())
                    {
                        response.Success = true;
                        response.Message = "Comment added succesfully.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch
            {
                response         = new LacesResponse();
                response.Success = false;
                response.Message = "An unexpected error has occurred; please verify the format of your request.";
            }

            return(response);
        }
Exemple #6
0
        public LacesResponse ChangeUserPassword(UpdatePasswordRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User(request.UserId);

                    user.Password = request.NewPassword;

                    if (user.UpdatePassword(request.OldPassword))
                    {
                        response.Success = true;
                        response.Message = "Update complete.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;

                if (ex.Message.Contains("incorrect") || ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #7
0
        public LacesResponse RemoveFromInterestQueue(ProductRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    // Confirm user and product exist
                    LacesDataModel.User.User       user    = new LacesDataModel.User.User(request.UserId);
                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId);

                    UserInterestQueue userInterest = new UserInterestQueue();

                    userInterest.LoadByUserAndProductIds(user.UserId, product.ProductId);

                    if (userInterest.UserInterestQueueId == 0)
                    {
                        userInterest.UserId     = user.UserId;
                        userInterest.ProductId  = product.ProductId;
                        userInterest.Interested = false;

                        if (userInterest.Add())
                        {
                            response.Success = true;
                            response.Message = "Operation completed.";
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "An error occurred when communicating with the database.";
                        }
                    }
                    else if (userInterest.Interested == true)
                    {
                        userInterest.Interested = false;

                        if (userInterest.Update())
                        {
                            response.Success = true;
                            response.Message = "Operation completed.";
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "An error occurred when communicating with the database.";
                        }
                    }
                    else
                    {
                        response.Success = true;
                        response.Message = "Operation completed.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new LacesResponse();
                response.Success = false;

                if (ex.Message.Contains("find user") || ex.Message.Contains("find product") || ex.Message.Contains("find like"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #8
0
        public AddUserResponse AddUser(AddUserRequest request)
        {
            AddUserResponse response = new AddUserResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User();

                    user.Description = request.Description;
                    user.DisplayName = request.DisplayName;
                    user.Email       = request.Email;
                    user.Password    = request.Password;
                    user.UserName    = request.UserName;

                    bool userNameValid = true;
                    bool emailValid    = true;

                    if (user.UserNameInUse())
                    {
                        userNameValid = false;
                    }

                    if (user.EmailInUse())
                    {
                        emailValid = false;
                    }

                    if (userNameValid && emailValid)
                    {
                        if (user.Add())
                        {
                            response.Success = true;
                            response.Message = "User succesfully created with Id: " + user.UserId;
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "An error occurred while processing your request.";
                        }
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "Username or email address is already in use.";

                        if (userNameValid == false)
                        {
                            response.UserNameTaken = true;
                        }

                        if (emailValid == false)
                        {
                            response.EmailTaken = true;
                        }
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch
            {
                response         = new AddUserResponse();
                response.Success = false;
                response.Message = "An unexpected error has occurred; please verify the format of your request.";
            }

            return(response);
        }
Exemple #9
0
        public LacesResponse UpdateProfileImage(AddImageRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User(request.AssociatedEntityId);

                    string fileName = user.UserName + "." + request.ImageInfo.FileFormat;
                    string filePath = ConfigurationManager.AppSettings[Constants.APP_SETTING_USER_AVATAR_DIRECTORY] + fileName;

                    File.WriteAllBytes(filePath, request.ImageInfo.ImageData);

                    Image userAvatar = new Image();

                    bool success;

                    if (userAvatar.LoadAvatarByUserId(user.UserId))
                    {
                        userAvatar.FileFormat  = request.ImageInfo.FileFormat;
                        userAvatar.UpdatedDate = DateTime.Now;
                        success = userAvatar.Update();
                    }
                    else
                    {
                        userAvatar.AssociatedEntityId = user.UserId;
                        userAvatar.FileFormat         = request.ImageInfo.FileFormat;
                        userAvatar.FileName           = fileName;
                        userAvatar.FilePath           = filePath;
                        userAvatar.ImageEntityTypeId  = (int)ImageEntityTypeOptions.User;
                        userAvatar.CreatedDate        = DateTime.Now;
                        userAvatar.UpdatedDate        = DateTime.Now;
                        success = userAvatar.Add();
                    }

                    if (success)
                    {
                        response.Success = true;
                        response.Message = "User profile picture succesfully updated.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #10
0
        public LacesResponse FollowUser(UserFollowRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User followedUser  = new LacesDataModel.User.User(request.FollowedUserId);
                    LacesDataModel.User.User followingUser = new LacesDataModel.User.User(request.UserId);

                    UserFollow follow = new UserFollow();

                    follow.LoadByUserids(followingUser.UserId, followedUser.UserId); // Make sure follow does not already exist.

                    if (follow.UserFollowId == 0)
                    {
                        follow.FollowedUserId  = followedUser.UserId;
                        follow.FollowingUserId = followingUser.UserId;
                        follow.CreatedDate     = DateTime.Now;

                        if (follow.Add())
                        {
                            followedUser.UsersFollowing++;
                            followedUser.Update();

                            followingUser.UsersFollowed++;
                            followingUser.Update();

                            response.Success = true;
                            response.Message = "Operation completed successfully.";
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "Failed to add user follow.";
                        }
                    }
                    else
                    {
                        response.Success = true;
                        response.Message = "User is already being followed.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #11
0
        public LacesResponse UpdateUserInfo(UpdateUserInfoRequest request)
        {
            LacesResponse response = new LacesResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User(request.UserId);

                    bool changed = false;

                    if (request.Description != null && request.Description != user.Description)
                    {
                        user.Description = request.Description;
                        changed          = true;
                    }

                    if (request.DisplayName != null && request.DisplayName != user.DisplayName)
                    {
                        user.DisplayName = request.DisplayName;
                        changed          = true;
                    }

                    if (changed)
                    {
                        if (user.Update())
                        {
                            response.Success = true;
                            response.Message = "User updated succesfully.";
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "An error occurred when communicating with the database.";
                        }
                    }
                    else
                    {
                        response.Success = true;
                        response.Message = "No changes made.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new LacesResponse();
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #12
0
        public GetUserResponse GetUser([FromBody] GetUserRequest request)
        {
            GetUserResponse response = new GetUserResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User userResult = new LacesDataModel.User.User(request.UserIdToGet);

                    response.User = new LacesAPI.Models.Response.User();

                    response.User.CreatedDate = userResult.CreatedDate;
                    response.User.Description = userResult.Description;

                    if (string.IsNullOrEmpty(userResult.DisplayName) == false)
                    {
                        response.User.DisplayName = userResult.DisplayName;
                    }
                    else
                    {
                        response.User.DisplayName = userResult.UserName;
                    }

                    response.User.Email          = userResult.Email;
                    response.User.UserName       = userResult.UserName;
                    response.User.FollowedUsers  = userResult.UsersFollowed;
                    response.User.FollowingUsers = userResult.UsersFollowing;
                    response.User.ProfilePicture = new LacesAPI.Models.Response.ImageInfo();

                    Image profPic = new Image();

                    if (profPic.LoadAvatarByUserId(userResult.UserId))
                    {
                        response.User.ProfilePicture.DateLastChanged = profPic.UpdatedDate;
                        response.User.ProfilePicture.FileFormat      = profPic.FileFormat;
                        response.User.ProfilePicture.FileName        = profPic.FileName;
                        response.User.ProfilePicture.FileData        = File.ReadAllBytes(profPic.FilePath);
                    }
                    else
                    {
                        response.User.ProfilePicture.DateLastChanged = userResult.CreatedDate;
                        response.User.ProfilePicture.FileFormat      = ConfigurationManager.AppSettings[Constants.APP_SETTING_DEFAULT_PROFILE_PIC_FORMAT];
                        response.User.ProfilePicture.FileName        = ConfigurationManager.AppSettings[Constants.APP_SETTING_DEFAULT_PROFILE_PIC_NAME];
                        response.User.ProfilePicture.FileData        = File.ReadAllBytes(ConfigurationManager.AppSettings[Constants.APP_SETTING_DEFAULT_PROFILE_PIC_PATH]);
                    }

                    response.User.Products = new List <int>();

                    List <LacesDataModel.Product.Product> products = LacesDataModel.Product.Product.GetProductsForUser(userResult.UserId);

                    foreach (LacesDataModel.Product.Product prod in products)
                    {
                        response.User.Products.Add(prod.ProductId);
                    }

                    response.User.ProductCount = response.User.Products.Count();

                    if (userResult.UserId != request.UserId)
                    {
                        UserFollow isBeingFollowed = new UserFollow();
                        isBeingFollowed.LoadByUserids(request.UserId, userResult.UserId);

                        if (isBeingFollowed.UserFollowId > 0)
                        {
                            response.IsBeingFollowed = true;
                        }
                        else
                        {
                            response.IsBeingFollowed = false;
                        }


                        UserFollow isFollowing = new UserFollow();
                        isFollowing.LoadByUserids(userResult.UserId, request.UserId);

                        if (isFollowing.UserFollowId > 0)
                        {
                            response.IsFollowing = true;
                        }
                        else
                        {
                            response.IsFollowing = false;
                        }
                    }
                    else
                    {
                        response.IsBeingFollowed = false;
                        response.IsFollowing     = false;
                    }

                    response.Success = true;
                    response.Message = "User details retrieved succesfully.";
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new GetUserResponse();
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #13
0
        public GetDetailedProductResponse GetDetailedProduct(ProductRequest request)
        {
            GetDetailedProductResponse response = new GetDetailedProductResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId);

                    if (product.ProductStatusId != (int)ProductStatusOptions.Removed)
                    {
                        LacesDataModel.User.User user = new LacesDataModel.User.User(product.SellerId);

                        response.Product = new LacesAPI.Models.Response.Product();

                        response.Product.AskingPrice = product.AskingPrice;
                        response.Product.Brand       = product.Brand;
                        response.Product.Comments    = new List <int>();

                        List <Comment> comments = Comment.GetCommentsForProduct(product.ProductId);

                        foreach (Comment comment in comments)
                        {
                            response.Product.Comments.Add(comment.CommentId);
                        }

                        response.Product.CommentCount = response.Product.Comments.Count;
                        response.Product.ConditionId  = product.ConditionId;
                        response.Product.CreatedDate  = product.CreatedDate;
                        response.Product.Description  = product.Description;

                        List <UserLike> likes = UserLike.GetLikesForProduct(product.ProductId); // Consider adding aggregate functions to repo classes

                        response.Product.LikeCount     = likes.Count;
                        response.Product.Name          = product.Name;
                        response.Product.ProductImages = new List <LacesAPI.Models.Response.ImageInfo>();

                        List <Image> images = Image.GetImagesForProduct(product.ProductId);

                        foreach (Image image in images)
                        {
                            LacesAPI.Models.Response.ImageInfo imageInfo = new LacesAPI.Models.Response.ImageInfo();

                            imageInfo.DateLastChanged = image.UpdatedDate;
                            imageInfo.FileData        = File.ReadAllBytes(image.FilePath);
                            imageInfo.FileFormat      = image.FileFormat;
                            imageInfo.FileFormat      = image.FileName;

                            response.Product.ProductImages.Add(imageInfo);
                        }

                        response.Product.Size = product.Size;
                        response.UserName     = user.UserName;

                        Image userImage = new Image();

                        userImage.LoadAvatarByUserId(user.UserId);

                        response.UserProfilePic = new LacesAPI.Models.Response.ImageInfo();
                        response.UserProfilePic.DateLastChanged = userImage.UpdatedDate;
                        response.UserProfilePic.FileData        = File.ReadAllBytes(userImage.FilePath);
                        response.UserProfilePic.FileFormat      = userImage.FileFormat;
                        response.UserProfilePic.FileName        = userImage.FileName;
                        response.Product.Tags = new List <LacesAPI.Models.Response.Tag>();

                        List <LacesDataModel.Product.Tag> tags = LacesDataModel.Product.Tag.GetTagsForProduct(product.ProductId);

                        foreach (LacesDataModel.Product.Tag tag in tags)
                        {
                            LacesAPI.Models.Response.Tag respTag = new LacesAPI.Models.Response.Tag();

                            respTag.TagId       = tag.TagId;
                            respTag.Description = tag.Description;

                            response.Product.Tags.Add(respTag);
                        }

                        UserInterestQueue interest = new UserInterestQueue();

                        interest.LoadByUserAndProductIds(user.UserId, product.ProductId);

                        if (interest.UserInterestQueueId > 0)
                        {
                            if (interest.Interested)
                            {
                                response.UserInterestStatus = (int)UserInterestStatusOption.Interested;
                            }
                            else
                            {
                                response.UserInterestStatus = (int)UserInterestStatusOption.Uninterested;
                            }
                        }
                        else
                        {
                            response.UserInterestStatus = (int)UserInterestStatusOption.Unknown;
                        }

                        response.Success = true;
                        response.Message = "Product details retrieved succesfully.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "That product has been removed and cannot be updated.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new GetDetailedProductResponse();
                response.Success = false;

                if (ex.Message.Contains("find user") || ex.Message.Contains("find product"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #14
0
        public GetShortProductResponse GetShortProduct(ProductRequest request)
        {
            GetShortProductResponse response = new GetShortProductResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product(request.ProductId);

                    if (product.ProductStatusId != (int)ProductStatusOptions.Removed)
                    {
                        LacesDataModel.User.User user = new LacesDataModel.User.User(product.SellerId);

                        List <Comment> comments = Comment.GetCommentsForProduct(product.ProductId);

                        response.CommentCount = comments.Count;

                        List <UserLike> likes = UserLike.GetLikesForProduct(product.ProductId); // Consider adding aggregate functions to repo classes

                        response.LikeCount    = likes.Count;
                        response.ProductImage = new LacesAPI.Models.Response.ImageInfo();

                        List <Image> images = Image.GetImagesForProduct(product.ProductId);

                        if (images.Count > 0)
                        {
                            response.ProductImage.DateLastChanged = images[0].UpdatedDate;
                            response.ProductImage.FileData        = File.ReadAllBytes(images[0].FilePath);
                            response.ProductImage.FileFormat      = images[0].FileFormat;
                            response.ProductImage.FileFormat      = images[0].FileName;
                        }

                        response.UserName = user.UserName;

                        Image userImage = new Image();

                        userImage.LoadAvatarByUserId(user.UserId);

                        response.UserProfilePic = new LacesAPI.Models.Response.ImageInfo();
                        response.UserProfilePic.DateLastChanged = userImage.UpdatedDate;
                        response.UserProfilePic.FileData        = File.ReadAllBytes(userImage.FilePath);
                        response.UserProfilePic.FileFormat      = userImage.FileFormat;
                        response.UserProfilePic.FileName        = userImage.FileName;

                        response.Success = true;
                        response.Message = "Product details retrieved succesfully.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "That product has been removed and cannot be updated.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new GetShortProductResponse();
                response.Success = false;

                if (ex.Message.Contains("find user") || ex.Message.Contains("find product"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }
Exemple #15
0
        public AddProductResponse AddProduct(AddProductRequest request)
        {
            AddProductResponse response = new AddProductResponse();

            try
            {
                if (request.SecurityString == ConfigurationManager.AppSettings[Constants.APP_SETTING_SECURITY_TOKEN])
                {
                    LacesDataModel.User.User user = new LacesDataModel.User.User(request.UserId); // Ensure user exists.

                    LacesDataModel.Product.Product product = new LacesDataModel.Product.Product();
                    product.AskingPrice     = request.AskingPrice;
                    product.Brand           = request.Brand;
                    product.ConditionId     = request.ConditionId;
                    product.CreatedDate     = DateTime.Now;
                    product.Description     = request.Description;
                    product.Name            = request.ProductName;
                    product.ProductStatusId = (int)ProductStatusOptions.Open;
                    product.ProductTypeId   = request.ProductTypeId;
                    product.SellerId        = user.UserId;
                    product.Size            = request.Size;
                    product.UpdatedDate     = DateTime.Now;

                    if (product.Add())
                    {
                        if (request.Images != null && request.Images.Count > 0)
                        {
                            int count = 0;

                            /* This is needed until we can get the Id back from Add(). Remove ASAP */

                            List <LacesDataModel.Product.Product> queryResult = new List <LacesDataModel.Product.Product>();

                            SearchEntity search = new SearchEntity();

                            search.ColumnsToReturn = new List <string>();
                            search.ColumnsToReturn.Add("ProductId");

                            search.ConnectionString = Constants.CONNECTION_STRING;

                            search.OrderBy           = new OrderBy();
                            search.OrderBy.Column    = "ProductId";
                            search.OrderBy.Direction = OrderByDirection.DESC;

                            search.PageSizeLimit = 1;

                            search.SchemaName = Constants.SCHEMA_DEFAULT;
                            search.TableName  = Constants.TABLE_PRODUCTS;

                            queryResult = new GenericRepository <LacesDataModel.Product.Product>().Read(search);

                            if (queryResult.Count > 0)
                            {
                                product.ProductId = queryResult[0].ProductId;
                            }

                            /* End remove section */

                            foreach (LacesAPI.Models.Request.ImageInfo image in request.Images)
                            {
                                string fileName = product.ProductId + "_" + count + "." + image.FileFormat;
                                string filePath = ConfigurationManager.AppSettings[Constants.APP_SETTING_PRODUCT_IMAGE_DIRECTORY] + fileName;

                                File.WriteAllBytes(filePath, image.ImageData);

                                Image productImage = new Image();

                                productImage.AssociatedEntityId = product.ProductId;
                                productImage.FileFormat         = image.FileFormat;
                                productImage.FileName           = fileName;
                                productImage.FilePath           = filePath;
                                productImage.ImageEntityTypeId  = (int)ImageEntityTypeOptions.Product;
                                productImage.CreatedDate        = DateTime.Now;
                                productImage.UpdatedDate        = DateTime.Now;
                                productImage.Add();
                            }
                        }

                        if (request.Tags != null && request.Tags.Count > 0)
                        {
                            foreach (string tag in request.Tags)
                            {
                                LacesDataModel.Product.Tag newTag = new LacesDataModel.Product.Tag();

                                newTag.ProductId   = product.ProductId;
                                newTag.Description = tag;

                                newTag.Add();
                            }
                        }

                        response.Success = true;
                        response.Message = "Product succesfully created.";
                    }
                    else
                    {
                        response.Success = false;
                        response.Message = "An error occurred when communicating with the database.";
                    }
                }
                else
                {
                    response.Success = false;
                    response.Message = "Invalid security token.";
                }
            }
            catch (Exception ex)
            {
                response         = new AddProductResponse();
                response.Success = false;

                if (ex.Message.Contains("find user"))
                {
                    response.Message = ex.Message;
                }
                else
                {
                    response.Message = "An unexpected error has occurred; please verify the format of your request.";
                }
            }

            return(response);
        }