예제 #1
0
        public async Task <IActionResult> DeleteNews(int id)
        {
            try
            {
                var token = Request.Headers["Authorization"].ToString();

                var deletedEntry = await NewsService.GetByIdAsync <News>(id);

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

                if (JwtHelper.CheckIfUserIsModerator(token) && deletedEntry.PublishedById != JwtHelper.GetUserIdFromJwt(token))
                {
                    return(Forbid());
                }

                await NewsService.Delete <News>(id);

                return(Ok(new Response
                {
                    Status = ResponseType.Successful
                }));
            }
            catch
            {
                return(Ok(new Response {
                    Status = ResponseType.Failed
                }));
            }
        }
예제 #2
0
        public async Task <IActionResult> ChangeNewsCategory(int newsId, int categoryId)
        {
            try
            {
                var token = Request.Headers["Authorization"].ToString();

                var news = await NewsService.GetByIdAsync <News>(newsId);

                var category = await CategoryService.GetByIdAsync <Category>(categoryId);

                if (category == null)
                {
                    return(Ok(new Response {
                        Status = ResponseType.Failed, Message = "Category not found"
                    }));
                }
                if (news == null)
                {
                    return(Ok(new Response {
                        Status = ResponseType.Failed, Message = "News Request not found"
                    }));
                }
                if (JwtHelper.CheckIfUserIsModerator(token) && news.PublishedById != JwtHelper.GetUserIdFromJwt(token))
                {
                    return(Forbid());
                }

                var result = await NewsService.ChangeNewsCategoryAsync(news, category);

                if (!result)
                {
                    return(Ok(new Response {
                        Status = ResponseType.Failed
                    }));
                }

                return(Ok(new Response
                {
                    Status = ResponseType.Successful
                }));
            }
            catch
            {
                return(Ok(new Response {
                    Status = ResponseType.Failed
                }));
            }
        }
예제 #3
0
        public async Task <IActionResult> UploadImage(int newsId, IFormFile file)
        {
            try
            {
                var token = Request.Headers["Authorization"].ToString();

                var news = await NewsService.GetByIdAsync <News>(newsId);

                if (news == null)
                {
                    return(NotFound());
                }
                if (JwtHelper.CheckIfUserIsModerator(token) && news.PublishedById != JwtHelper.GetUserIdFromJwt(token))
                {
                    return(Forbid());
                }

                var imageUrl = await ImageHandler.UploadImage(file);

                if (string.IsNullOrEmpty(imageUrl))
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }

                var image = new Image {
                    News   = news,
                    NewsId = newsId,
                    Url    = imageUrl
                };

                var imageId = await ImageService.AddAsync(image);

                if (imageId > 0 && !string.IsNullOrEmpty(token))
                {
                    await ImageService.PublishEntity <Image>(imageId, JwtHelper.GetUserIdFromJwt(token), JwtHelper.GetUserNameFromJwt(token));
                }

                return(Ok(new Response {
                    Status = ResponseType.Successful
                }));
            }
            catch
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
예제 #4
0
        public async Task <IActionResult> EditComment(int commentId, [FromBody] Comment comment)
        {
            try
            {
                var token = Request.Headers["Authorization"].ToString();

                var updatedEntry = await CommentService.GetByIdAsync <Comment>(commentId);

                if (updatedEntry == null)
                {
                    return(NotFound());
                }
                if (JwtHelper.CheckIfUserIsMember(token) && updatedEntry.PublishedById != JwtHelper.GetUserIdFromJwt(token))
                {
                    return(Forbid());
                }
                if (JwtHelper.CheckIfUserIsModerator(token))
                {
                    var news = await NewsService.GetByIdAsync <News>(comment.NewsId);

                    if (news.PublishedById != JwtHelper.GetUserIdFromJwt(token))
                    {
                        return(Forbid());
                    }
                }

                await CommentService.UpdateAsync(commentId, comment);

                if (!string.IsNullOrEmpty(token))
                {
                    await CommentService.UpdateLastEdit <Comment>(commentId, JwtHelper.GetUserIdFromJwt(token), JwtHelper.GetUserNameFromJwt(token));
                }

                return(Ok(new Response
                {
                    Status = ResponseType.Successful,
                    Value = comment
                }));
            }
            catch
            {
                return(Ok(new Response {
                    Status = ResponseType.Failed
                }));
            }
        }