Esempio n. 1
0
        public IHttpActionResult PostPost(CreatePostModel model)
        {
            try
            {
                var userService          = new UserService();
                var userAccountSuspended = userService.VerifyIfIsSuspendedAndUpdateUser(new VerifyIfIsSuspendedAndUpdateUserRequest {
                    AspNetUserId = User.Identity.GetUserId()
                }).UserSuspended;

                if (userAccountSuspended)
                {
                    return(BadRequest("User account suspended"));
                }

                var user = userService.GetUserByAccountId(new GetUserByAccountIdRequest {
                    AccountId = User.Identity.GetUserId()
                }).User;

                if (user != null)
                {
                    model.IdWriter = user.Id;
                }

                if (ModelState.IsValid)
                {
                    var postService = new PostService();

                    string   finalFileName = string.Empty;
                    string   extension     = string.Empty;
                    string[] tokens        = new string[1];

                    if (!string.IsNullOrEmpty(model.base64File) && !string.IsNullOrWhiteSpace(model.base64File))
                    {
                        tokens    = model.base64File.Split(',');
                        extension = tokens[0].Split('/')[1].Split(';')[0];
                    }

                    // Verifica si el modelo no tiene imagen, o tiene imagen y la misma tiene una extensión permitida.
                    if ((string.IsNullOrEmpty(model.base64File) && string.IsNullOrWhiteSpace(model.base64File)) ||
                        (!string.IsNullOrEmpty(extension) &&
                         new AllowedExtensions().ImageExtensions.Contains(extension.ToLower()))
                        )
                    {
                        var request = new CreatePostRequest
                        {
                            IdWriter   = model.IdWriter,
                            Title      = model.Title,
                            Summary    = model.Summary,
                            Body       = model.Body,
                            CategoryId = model.IdCategory,
                            IsDraft    = model.IsDraft,
                            Tags       = model.Tags
                        };

                        var createResult = postService.CreatePost(request);

                        if (!string.IsNullOrEmpty(extension))
                        {
                            var folder = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PathPostPrincipalImages"]);
                            finalFileName = string.Format("{0}.{1}", createResult.PostId.ToString(), extension);

                            string path   = Path.Combine(folder, finalFileName);
                            var    stream = ImageHelpers.Base64ToStream(tokens[1], path);
                            var    image  = new Bitmap(stream);

                            // Se guarda la imagen en el servidor.
                            image.Save(path);

                            // Se actualiza el post con la imagen que se acaba de guardar.
                            var updateResult = postService.UpdatePost(new UpdatePostRequest
                            {
                                Id                 = createResult.PostId,
                                Title              = model.Title,
                                Summary            = model.Summary,
                                Body               = model.Body,
                                IdCategory         = model.IdCategory,
                                IsDraft            = model.IsDraft,
                                PrincipalImageName = "api/" + finalFileName,
                                Tags               = model.Tags
                            });
                        }

                        return(Ok());
                    }
                    else
                    {
                        return(BadRequest("La extensión de la imagen no es válida, vuelva a cargarla."));
                    }
                }
                return(BadRequest(ModelState));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public IHttpActionResult PutPost(EditPostModel model)
        {
            try
            {
                var userService          = new UserService();
                var userAccountSuspended = userService.VerifyIfIsSuspendedAndUpdateUser(new VerifyIfIsSuspendedAndUpdateUserRequest {
                    AspNetUserId = User.Identity.GetUserId()
                }).UserSuspended;

                if (userAccountSuspended)
                {
                    return(BadRequest("User account suspended"));
                }

                var    postService      = new PostService();
                var    complaintService = new ComplaintService();
                string finalFileName    = string.Empty;

                var postComplaints = complaintService.SearchComplaintsByPostId(new SearchComplaintsByPostIdRequest {
                    PostId = model.Id
                }).Complaints;

                if (postComplaints.Any())
                {
                    return(BadRequest("Post with complaints"));
                }

                if (ModelState.IsValid)
                {
                    var post = postService.GetPostById(new GetPostByIdRequest()
                    {
                        Id = model.Id
                    }).Post;

                    if (post == null)
                    {
                        return(NotFound());
                    }

                    var user = userService.GetUserByAccountId(new GetUserByAccountIdRequest {
                        AccountId = User.Identity.GetUserId()
                    }).User;

                    if (post.IdWriter != user.Id)
                    {
                        return(BadRequest());
                    }

                    if (!string.IsNullOrEmpty(model.base64File) && !string.IsNullOrWhiteSpace(model.base64File) && !model.DeleteImage)
                    {
                        string[] tokens    = model.base64File.Split(',');
                        string   extension = tokens[0].Split('/')[1].Split(';')[0];

                        if (new AllowedExtensions().ImageExtensions.Contains(extension.ToLower()))
                        {
                            var folder = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PathPostPrincipalImages"]);
                            finalFileName = string.Format("{0}.{1}", model.Id.ToString(), extension);

                            string path   = Path.Combine(folder, finalFileName);
                            var    stream = ImageHelpers.Base64ToStream(tokens[1], path);
                            var    image  = new Bitmap(stream);

                            // Se guarda la imagen en el servidor.
                            image.Save(path);

                            finalFileName = "api/" + finalFileName;
                        }
                        else
                        {
                            ModelState.AddModelError("", "La extensión de la imagen no es válida, vuelva a cargarla.");
                        }
                    }

                    var result = postService.UpdatePost(new UpdatePostRequest
                    {
                        Id                 = model.Id,
                        Title              = model.Title,
                        Summary            = model.Summary,
                        Body               = model.Body,
                        IdCategory         = model.IdCategory,
                        IsDraft            = model.IsDraft,
                        PrincipalImageName = string.IsNullOrEmpty(finalFileName) && !model.DeleteImage ? post.PrincipalImageName : finalFileName,
                        Tags               = model.Tags
                    });

                    return(Ok());
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }