Esempio n. 1
0
        public async Task <ForumThread> Update(int userId, int forumThreadId, ForumThreadForUpdateDto forumThread, bool sudo)
        {
            var user = await context
                       .Users
                       .FirstOrDefaultAsync(u => u.Id == userId);

            if (user == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }

            var forumThreadEntity = await context
                                    .ForumThreads
                                    .FirstOrDefaultAsync(t => t.Id == forumThreadId);

            if (forumThreadEntity == null)
            {
                throw new ArgumentNullException(nameof(forumThreadId));
            }

            if (user.Id != forumThreadEntity.AuthorId && !sudo)
            {
                throw new AuthenticationException();
            }

            forumThreadEntity.Title = forumThread.Title;

            context.Entry(forumThreadEntity).State = EntityState.Modified;
            await context.SaveChangesAsync();

            return(forumThreadEntity);
        }
Esempio n. 2
0
        public async Task <IActionResult> PutForumThread(int forumThreadId, ForumThreadForUpdateDto forumThread)
        {
            try
            {
                var user = await GetUser();

                var forumThreadUpdated = await context.Update(user.Id, forumThreadId, forumThread, IsSudo());

                return(Ok(mapper.Map <ForumThreadPreviewDto>(forumThreadUpdated)));
            }
            catch (ArgumentNullException e)
            {
                var paramName = e.ParamName;

                if (paramName != null)
                {
                    return(NotFound(paramName));
                }

                throw;
            }
            catch (AuthenticationException)
            {
                return(Forbid());
            }
        }