public async Task <IActionResult> CreatePost(CreatePostDTO createPostDTO)
        {
            var userId = GetUserId();

            // Does User exist?
            if (await UserExists(userId) != true)
            {
                return(Unauthorized());
            }

            var thread = await _Repo.Threads.FirstOrDefaultAsync(x => x.Id == createPostDTO.ThreadId);

            var newPost = new Post
            {
                Content   = JsonSerializer.Serialize(createPostDTO.Content),
                CreatedOn = DateTime.Now,
                ThreadFK  = thread.Id,
                Thread    = thread,
                AuthorId  = int.Parse(userId)
            };

            thread.LastUpdated = DateTime.Now;

            _Repo.Add(newPost);
            _Repo.Update(thread);

            await _Repo.SaveChangesAsync();

            return(await GetPosts(createPostDTO.ThreadId));
        }
示例#2
0
        public async Task <IActionResult> UpdateThread(UpdateThreadDTO updateThreadDTO, int threadId)
        {
            var threadToUpdate = await _Repo.Threads.FirstOrDefaultAsync(x => x.Id == threadId);

            if (threadToUpdate == null)
            {
                return(BadRequest("Thread does not exist"));
            }

            var userId = GetUserId();

            if (await UserExists(userId) == false)
            {
                return(Unauthorized());
            }

            if (IsAdmin() != true || threadToUpdate.AuthorId != int.Parse(userId))
            {
                return(Unauthorized());
            }

            threadToUpdate.Title = updateThreadDTO.Title;

            if (threadToUpdate.Content != updateThreadDTO.Content)
            {
                threadToUpdate.Content      = updateThreadDTO.Content;
                threadToUpdate.Edited       = true;
                threadToUpdate.LastEditDate = DateTime.Now;
            }

            _Repo.Update(threadToUpdate);
            await _Repo.SaveChangesAsync();

            return(Ok("Thread successfully updated"));
        }
        public async Task <IActionResult> UpdateForum(UpdateForumDTO updateForumDTO, int forumId)
        {
            var forumToUpdate = await _Repo.Forums.FirstOrDefaultAsync(x => x.Id == forumId);

            if (forumToUpdate == null)
            {
                return(BadRequest("Forum not found!"));
            }

            forumToUpdate.Title       = updateForumDTO.Title;
            forumToUpdate.Slug        = updateForumDTO.Title.GenerateSlug();
            forumToUpdate.Description = updateForumDTO.Description ?? string.Empty;

            _Repo.Update(forumToUpdate);
            await _Repo.SaveChangesAsync();

            return(await GetForums());
        }
        public async Task <IActionResult> UpdateSubforum(UpdateSubforumDTO updateSubforumDTO, int subforumId)
        {
            var subforumToUpdate = await _Repo.Subforums.FirstOrDefaultAsync(x => x.Id == subforumId);

            if (subforumToUpdate == null)
            {
                return(BadRequest("Subforum does not exist"));
            }

            subforumToUpdate.Title       = updateSubforumDTO.Title;
            subforumToUpdate.Slug        = updateSubforumDTO.Title.GenerateSlug();
            subforumToUpdate.Description = updateSubforumDTO.Description;
            subforumToUpdate.Icon        = updateSubforumDTO.ImageUrl;

            _Repo.Update(subforumToUpdate);
            await _Repo.SaveChangesAsync();

            return(await GetForumsAsync());
        }