public ActionResult AddPost([FromBody] PostVm postVm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var post = _mapper.Map <PostVm, Post>(postVm);
                    _mbaContext.Posts.Add(post);
                    if (_mbaContext.SaveChanges() > 0)
                    {
                        return(Created($"/api/posts/addPost{postVm.PostId}", post));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to add new post to database: {ex}");
            }

            return(BadRequest("Failed to add new post to database"));
        }
Example #2
0
        public bool Update(int id, PostVm vm)
        {
            var entity = _uow.Call <Post>()
                         .Query(x => x.Id == id)
                         .Include(x => x.Tags)
                         .SingleOrDefault();

            entity.Title   = vm.Title;
            entity.Content = vm.Content;

            foreach (var item in vm.Tags)
            {
                var tag = _uow.Call <Tag>().Find(item.Id);
                switch (item.State)
                {
                case ItemChangeState.Added:
                    _uow.Call <Post>().AddRelationship(entity, x => x.Tags, tag);
                    break;

                case ItemChangeState.Deleted:
                    _uow.Call <Post>().RemoveRelationship(entity, x => x.Tags, tag);
                    break;
                }
            }

            _uow.Call <Post>().Update(entity);
            return(_uow.Commit());
        }
        public async Task <IActionResult> Put(int communityId, int postId, [FromBody] PostVm value)
        {
            var post = Map(value);

            post.Author = await _dbContext.Users.FindAsync(post.Author.Id);

            _dbContext.Posts.Update(post);
            await _dbContext.SaveChangesAsync();

            return(NoContent());
        }
Example #4
0
        public ActionResult Get(string id)
        {
            var methodResult = _postService.GetPostById(id);
            var result       = PostVm.WrapToPostVm(methodResult);

            return(Ok(new
            {
                Status = 1,
                Result = result,
                Error = ""
            }));
        }
        // [TypeFilter(typeof(ValidateUserIsPostAuthorFilterAttribute))]
        public async Task <IActionResult> Post(int communityId, [FromBody] PostVm value)
        {
            var post = Map(value);

            post.Author = await _dbContext.Users.FindAsync(post.Author.Id);

            await _dbContext.Posts.AddAsync(post);

            await _dbContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new
            {
                CommunityId = communityId,
                PostId = post.Id
            }, Map(post)));
        }
Example #6
0
        public async ValueTask <IActionResult> Send([FromBody] PostVm post)
        {
            var currentUser = await db.Users.FirstOrDefaultAsync(u =>
                                                                 u.UserName == User.FindFirstValue(ClaimTypes.NameIdentifier));

            var postTemp = new Post
            {
                Text   = post.Text,
                Sender = currentUser,
                Time   = DateTime.Now
            };
            await db.AddAsync(postTemp);

            await db.SaveChangesAsync();

            return(Ok());
        }
Example #7
0
        public async ValueTask <IActionResult> SendComment(long id, [FromBody] PostVm post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id == default)
            {
                return(BadRequest());
            }
            var superPost = await db.Posts.FindAsync(id);

            if (superPost == default)
            {
                return(BadRequest());
            }
            var currentUser = await db.Users.FirstOrDefaultAsync(u =>
                                                                 u.UserName == User.FindFirstValue(ClaimTypes.NameIdentifier));

            var hasAccess = currentUser?.FollowedFollows?
                            .Any(f => f.Followed == superPost.Sender && f.Accepted) ?? false ||
                            User.IsInRole("Admin");

            if (!hasAccess)
            {
                return(Forbid());
            }
            var postTemp = new Post
            {
                Text   = post.Text,
                Sender = currentUser,
                Time   = DateTime.Now,
                Parent = superPost
            };
            await db.AddAsync(postTemp);

            await db.SaveChangesAsync();

            return(Ok());
        }
Example #8
0
        public bool Create(PostVm vm)
        {
            _uow.Begin();

            var entity = new Post
            {
                Title       = vm.Title,
                Content     = vm.Content,
                CreateDate  = DateTime.Now,
                ShowCounter = 0,
                IsActive    = true
            };

            foreach (var item in vm.Tags)
            {
                var tag = _uow.Call <Tag>().Find(item.Id);
                entity.Tags.Add(tag);
            }

            _uow.Call <Post>().Add(entity);
            return(_uow.Commit());
        }
Example #9
0
        public IActionResult Index()
        {
            List <PostVm> posts = new List <PostVm>();
            PostVm        post1 = new PostVm();

            post1.Title       = "Title1";
            post1.Description = "Description 1";
            post1.User        = "******";
            post1.AddDate     = "26 Sep, 2020";

            PostVm post2 = new PostVm();

            post2.Title       = "Title2";
            post2.Description = "Description 2";
            post2.User        = "******";
            post2.AddDate     = "28 Sep, 2020";

            PostVm post3 = new PostVm();

            post3.Title       = "Title3";
            post3.Description = "Description 3";
            post3.User        = "******";
            post3.AddDate     = "29 Sep, 2020";

            PostVm post4 = new PostVm();

            post4.Title       = "Title4";
            post4.Description = "Description 4";
            post4.User        = "******";
            post4.AddDate     = "30 Sep, 2020";

            posts.Add(post1);
            posts.Add(post2);
            posts.Add(post3);
            posts.Add(post4);


            return(View(posts));
        }
Example #10
0
        public IActionResult Article()
        {
            // Again, imagine some linq and sql stuff here to get actual data
            Person author = new Person()
            {
                name = "John",
                job  = "Editor at Large"
            };

            Blog story = new Blog()
            {
                title = "The world according to john",
                text  = "Everyone should listen to what I have to say, cuz I have the wisdom and stuff"
            };

            PostVm post = new PostVm()
            {
                blog   = story,
                author = author
            };

            return(View(post));
        }
        public PostPage(Item item)
        {
            InitializeComponent();
            Xamarin.Forms.PlatformConfiguration.iOSSpecific.Page.SetUseSafeArea(this, true);

            _viewModel = Resources["Vm"] as PostVm;
            _viewModel.SelectedPost = item;

            try
            {
                //throw (new Exception("Unable to load blog"));
                //WebView.Source = item.ItemLink;

                Title                 = item.Title;
                PostImage.Source      = item.Enclosure.Url;
                CreatorLabel.Text     = item.Creator;
                DateLabel.Text        = item.PublishedDate.ToString("MMMM dd");
                DescriptionLabel.Text = item.Description;

                var properties = new Dictionary <string, string>
                {
                    { "Blog_Post", $"{item.Title}" }
                };

                TrackEvent(properties);
            }
            catch (Exception ex)
            {
                var properties = new Dictionary <string, string>
                {
                    { "Blog_Post", $"{item.Title}" }
                };

                //Crashes.TrackError(ex, properties);
                TrackError(ex, properties);
            }
        }
Example #12
0
 /*
  * Post
  */
 /// <summary>
 /// Map using Automapper
 /// </summary>
 public static PostDto MapToDto(this PostVm PostVm)
 {
     return(Mapper.Map <PostVm, PostDto>(PostVm));
 }
Example #13
0
        public IHttpActionResult Update(int id, PostVm vm)
        {
            var result = _postBusiness.Update(id, vm);

            return(Ok(result));
        }
Example #14
0
        public IHttpActionResult Create(PostVm vm)
        {
            var result = _postBusiness.Create(vm);

            return(Ok(result));
        }
Example #15
0
 private Post Map(PostVm value) => _mapper.Map <Post>(value);