Esempio n. 1
0
        /// <summary>
        /// Obtenemos un post segun su id
        /// </summary>
        /// <param name="id">Id del post</param>
        /// <returns>Post</returns>
        public async Task <Response> GetPostById(int id)
        {
            Response response = new Response();

            try
            {
                using (var db = new ParchegramDBContext())
                {
                    Post post = await db.Post.Where(p => p.Id.Equals(id)).FirstOrDefaultAsync();

                    if (post == null)
                    {
                        return(response.GetResponse("No existe el post", 0, null));
                    }
                    PostListQueryResponse postListQueryResponse = await GetPostsQueryResponseById(id, db);

                    PostResponse postRensponse = await ProcessPostById(postListQueryResponse);

                    return(response.GetResponse($"Exito al traer el post", 1, postRensponse));
                }
            }
            catch (Exception e)
            {
                _logger.LogInformation(e.Message);
                return(response.GetResponse($"Ha ocurrido un error inesperado {e.Message}", 0, null));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Obtiene un Post segun su Id devolviendo el Post, el usuario que la subio y si se ha dado like
        /// </summary>
        /// <param name="idPost">Id del post</param>
        /// <param name="parchegramDBContext">Contexto de la base de datos</param>
        /// <returns>El post junto con el usario que lo subio y si se ha dado like</returns>
        private async Task <PostListQueryResponse> GetPostsQueryResponseById(int idPost, ParchegramDBContext parchegramDBContext)
        {
            PostListQueryResponse queryPostResponses = await(from tempPost in parchegramDBContext.Post

                                                             join tempUserOwner in parchegramDBContext.User on tempPost.IdUser equals tempUserOwner.Id

                                                             join tempLike in parchegramDBContext.Like on tempPost.Id equals tempLike.IdPost into leftTempLike
                                                             from subTempLike in leftTempLike.DefaultIfEmpty()

                                                             where tempPost.Id.Equals(idPost)

                                                             select new PostListQueryResponse
            {
                QueryPost      = tempPost,
                QueryUserOwner = tempUserOwner,
                QueryLike      = subTempLike
            }).FirstOrDefaultAsync();

            return(queryPostResponses);
        }
Esempio n. 3
0
        /// <summary>
        /// Se procesa un solo post
        /// </summary>
        /// <param name="postListQueryResponse">En este caso ya devolvemos el post ya que se ejecuta la consulta</param>
        /// <param name="byId">Id del post que quereoms consultar</param>
        /// <returns></returns>
        private async Task <PostResponse> ProcessPostById(PostListQueryResponse postListQueryResponse)
        {
            try
            {
                PostResponse     postResponse     = new PostResponse();
                ImageUserProfile imageUserProfile = new ImageUserProfile(false);
                ILikeService     likeService      = new LikeService();

                // Post
                postResponse.IdPost      = postListQueryResponse.QueryPost.Id;
                postResponse.IdTypePost  = postListQueryResponse.QueryPost.IdTypePost;
                postResponse.Description = postListQueryResponse.QueryPost.Description;
                postResponse.Date        = postListQueryResponse.QueryPost.Date;
                if (postListQueryResponse.QueryPost.PathFile != null)
                {
                    postResponse.File = await Image.GetFile(postListQueryResponse.QueryPost.PathFile);
                }

                // UserOwner
                postResponse.IdUserOwner           = postListQueryResponse.QueryUserOwner.Id;
                postResponse.NameUserOwner         = postListQueryResponse.QueryUserOwner.NameUser;
                postResponse.ImageProfileUserOwner = await imageUserProfile.GetImageUser(postListQueryResponse.QueryUserOwner.Id, 'S');

                postResponse.LikeUser = (postListQueryResponse.QueryLike != null) ? true : false;

                // Numero de likes de la publicación
                postResponse.NumberLikes = likeService.GetNumLikes(postListQueryResponse.QueryPost.Id);

                return(postResponse);
            }
            catch (Exception e)
            {
                _logger.LogInformation(e.Message);
                return(null);
            }
        }