Example #1
0
        public async Task <IActionResult> GetComments([FromQuery] GetCommentsRequest req)
        {
            Logger.LogInformation($"Get product comments with filter: ProductCode={req?.ProductCode}");

            int?productId = await Service.GetId(req.ProductCode).ConfigureAwait(false);

            if (!productId.HasValue)
            {
                return(NotFound($"ProductCode={req?.ProductCode}"));
            }

            var comments = await Service.GetComments(productId).ConfigureAwait(false);

            var result = new GetCommentResponse();

            if (comments != null)
            {
                result.Comments = new List <GetCommentResponse.Comment>();
                foreach (var comment in comments)
                {
                    result.Comments.Add(new GetCommentResponse.Comment
                    {
                        PosterName  = comment.PosterName,
                        ProductName = comment.ProductName,
                        Rating      = comment.Rating,
                        Description = comment.Description
                    });
                }
            }


            return(Ok(result));
        }
        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);
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            GetCommentResponse response = new GetCommentResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("comment", targetDepth))
                {
                    var unmarshaller = CommentUnmarshaller.Instance;
                    response.Comment = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }