Exemplo n.º 1
0
        public async Task <IActionResult> UpdateNews([FromRoute] int id, [FromBody] EditNewsViewModel model)
        {
            var result = default(ApiResult);

            try
            {
                var news = await context.News.FindAsync(id);

                if (news == null)
                {
                    result = ApiResult.CreateResult(ApiStatus.NotFound, "News not found");
                }
                else
                {
                    context.Entry <News>(news).CurrentValues.SetValues(model);
                    var save = await context.SaveChangesAsync();

                    result = ApiResult.CreateResult(ApiStatus.Ok, "Edited successfully");
                }
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);

                return(this.GetActionResult(result));
            }

            return(this.GetActionResult(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetNewsComments([FromRoute] int newsId)
        {
            var result = default(ApiResult);

            try
            {
                var news = await context.News.FindAsync(newsId);

                if (news == null)
                {
                    result = ApiResult.CreateResult(ApiStatus.NotFound, "News not found");
                }
                else
                {
                    var comments = context.Comments.Select(x => new CommentViewModel
                    {
                        Id      = x.Id,
                        Message = x.Message,
                        Name    = x.Name
                    }).ToArray();

                    result = ApiResult.CreateResult(ApiStatus.Ok, data: comments);
                }

                return(this.GetActionResult(result));
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);

                return(this.GetActionResult(result));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddNews([FromBody] CreateNewsViewModel model)
        {
            var result = default(ApiResult);

            var news = new News
            {
                Description = model.Description,
                ImagePath   = model.ImagePath,
                PostDate    = model.PostDate,
                Title       = model.Title
            };

            try
            {
                await context.News.AddAsync(news);

                await context.SaveChangesAsync();

                result = ApiResult.CreateResult(ApiStatus.Ok, "News added");
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);

                return(this.GetActionResult(result));
            }

            return(this.GetActionResult(result));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> GetById([FromRoute] int id)
        {
            var result = default(ApiResult);

            try
            {
                var news = await context.News.FindAsync(id);

                if (news == null)
                {
                    result = ApiResult.CreateResult(ApiStatus.NotFound, "News not found");
                }
                else
                {
                    var model = GetNewsViewModel(news);

                    result = ApiResult.CreateResult(ApiStatus.Ok, data: model);
                }
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);
                return(this.GetActionResult(result));
            }

            return(this.GetActionResult(result));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> RemoveNews([FromRoute] int id)
        {
            var result = default(ApiResult);

            try
            {
                var news = await context.News.FindAsync(id);

                if (news == null)
                {
                    result = ApiResult.CreateResult(ApiStatus.NotFound, "News not found");
                }
                else
                {
                    context.News.Remove(news);
                    var save = await context.SaveChangesAsync();

                    result = ApiResult.CreateResult(ApiStatus.Ok, "Removed successfully");
                }
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);
                return(this.GetActionResult(result));
            }

            return(this.GetActionResult(result));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> GetNews()
        {
            var result = default(ApiResult);

            var news = await context.News.ToArrayAsync();

            var models = news.Select(x => GetNewsViewModel(x));

            result = ApiResult.CreateResult(ApiStatus.Ok, data: models);

            return(this.GetActionResult(result));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> AddComment([FromRoute] int newsId, [FromBody] CreateCommentViewModel model)
        {
            var result = default(ApiResult);

            try
            {
                var news = await context.News.FindAsync(newsId);

                if (news == null)
                {
                    result = ApiResult.CreateResult(ApiStatus.NotFound, "News not found");
                }
                else
                {
                    var comment = new Comment
                    {
                        Message = model.Message,
                        Name    = model.Name,
                        News    = news
                    };

                    var resComment = await context.Comments.AddAsync(comment);

                    var save = await context.SaveChangesAsync();

                    result = ApiResult.CreateResult(ApiStatus.Ok, "Comment added");
                }

                return(this.GetActionResult(result));
            }
            catch (Exception ex)
            {
                result = ApiResult.CreateResult(ApiStatus.ApplicationError, ex.Message);

                return(this.GetActionResult(result));
            }
        }