public async Task <JsonResult> UpDatePost(PostModel postModel, IFormFile image)
        {
            try
            {
                var post = new MPosts();
                post.PostBody  = postModel.PostBody;
                post.UserEmail = postModel.UserEmail;
                post.Title     = postModel.Title;
                post.Id        = postModel.Id;

                var existingPost = db.MPosts.Find(post.Id);
                if (existingPost == null)
                {
                    return(Json(new
                    {
                        res = "err",
                        msg = $"Post with id={post.Id} does not exist"
                    }));
                }
                //delete old picture
                if (!string.IsNullOrEmpty(existingPost.ImageUrl))
                {
                    System.IO.File.Delete(existingPost.ImageUrl);
                }
                if (image != null)
                {
                    var filename = $"{Guid.NewGuid().ToString()}.{Path.GetExtension(image.FileName)}";
                    var filepath = $"{host.WebRootPath}/{uploads_folder}/{filename}";
                    using (var stream = System.IO.File.Create(filepath))
                    {
                        await image.CopyToAsync(stream);

                        stream.Dispose();
                    }
                    existingPost.ImageUrl = filename;
                }
                //update all the fields
                existingPost.Title           = post.Title;
                existingPost.PostBody        = post.PostBody;
                existingPost.UserEmail       = post.UserEmail;
                db.Entry(existingPost).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await db.SaveChangesAsync();

                return(Json(new
                {
                    res = "ok",
                    data = "Post updated"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    res = "err",
                    msg = ex.Message
                }));
            }
        }
        public async Task <JsonResult> UpDatePost(PostModel postModel)
        {
            try
            {
                var post = new MPosts();
                post.PostBody  = postModel.PostBody;
                post.UserEmail = postModel.UserEmail;
                post.Id        = postModel.Id;
                post.Title     = postModel.Title;

                var existingPost = db.MPosts.Find(post.Id);
                if (existingPost == null)
                {
                    return(Json(new
                    {
                        res = "err",
                        msg = $"Post with id={post.Id} does not exist"
                    }));
                }
                //update the fields that can change
                existingPost.Title           = post.Title;
                existingPost.PostBody        = post.PostBody;
                db.Entry(existingPost).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await db.SaveChangesAsync();

                return(Json(new
                {
                    res = "ok",
                    data = "Post updated"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    res = "err",
                    msg = ex.Message
                }));
            }
        }
        public async Task <JsonResult> CreatePost(IFormFile image, PostModel postModel)
        {
            try
            {
                var post = new MPosts();
                post.PostBody  = postModel.PostBody;
                post.UserEmail = postModel.UserEmail;
                post.Title     = postModel.Title;

                if (image != null)
                {
                    var filename = $"{Guid.NewGuid().ToString()}.{Path.GetExtension(image.FileName)}";
                    var filepath = $"{host.WebRootPath}/{uploads_folder}/{filename}";
                    using (var stream = System.IO.File.Create(filepath))
                    {
                        await image.CopyToAsync(stream);

                        stream.Dispose();
                    }
                    post.ImageUrl = filename;
                }
                db.MPosts.Add(post);
                db.SaveChanges();
                return(Json(new
                {
                    res = "ok",
                    data = "Post saved"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    res = "err",
                    msg = ex.Message
                }));
            }
        }