public async Task <ActionResult> Post([FromForm] FormPostModel formPost)
        {
            var imageUrl = string.Empty;

            if (formPost.File != null)
            {
                var imageName = $"{Guid.NewGuid().ToString()}{GetFileExtension(formPost.File.FileName)}";
                await storageService.UploadFile("posts", imageName, formPost.File);

                imageUrl = $"{storageUri}/posts/{imageName}";
            }

            var user = await userManager.GetUserAsync(this.User);

            dbContext.Post.Add(new Post
            {
                UserId          = user.Id,
                UserFullname    = user.UserName,
                PreviewImageUrl = imageUrl,
                Date            = DateTime.UtcNow,
                LikeCounter     = 0,
                Message         = formPost.Message
            });


            dbContext.SaveChanges();

            return(RedirectToAction("Index"));
        }
Пример #2
0
        public async Task <IActionResult> Edit(FormPostModel model)
        {
            if (!CustomValidator.Validate(model))
            {
                this.ViewData.Add("actionName", nameof(this.Edit));
                return(this.View("CreateOrEdit", model));
            }

            var now = DateTime.Now;

            model.UpdatedOn = now;

            await this.PostService.UpdatePost(model);

            return(this.RedirectToAction("Post", "Home", new { area = "", id = model.Id }));
        }
Пример #3
0
        public async Task <IActionResult> Edit(int id)
        {
            var pocoPost = await this.PostService.GetPostById(id);

            var model = new FormPostModel
            {
                Title     = pocoPost.Title,
                Content   = pocoPost.Content,
                Id        = pocoPost.Id,
                Tags      = string.Join(',', pocoPost.Tags.Select(tag => tag.Name)),
                UpdatedOn = pocoPost.UpdatedOn,
                CreatedOn = pocoPost.CreatedOn
            };

            this.ViewData.Add("actionName", nameof(this.Edit));
            return(this.View("CreateOrEdit", model));
        }
Пример #4
0
        public async Task <IActionResult> Create(FormPostModel inputModel)
        {
            var now = DateTime.Now;

            var model = new FormPostModel
            {
                Content   = inputModel.Content,
                Tags      = inputModel.Tags,
                Title     = inputModel.Title,
                UpdatedOn = now,
                CreatedOn = now,
            };

            if (!CustomValidator.Validate(model))
            {
                this.ViewData.Add("actionName", nameof(this.Create));
                return(this.View("CreateOrEdit", model));
            }

            int postId = await this.PostService.CreatePost(model);

            return(this.RedirectToAction("Post", "Home", new { area = "", id = postId }));
        }
Пример #5
0
        public IActionResult PostValues([FromBody] FormPostModel form)
        {
            var testVal = form;

            return(Ok("Succesfully posted"));
        }