Exemplo n.º 1
0
        public MPost Insert(PostInsertRequest request)
        {
            Post newPost = new Post
            {
                CategoryId   = request.Category.Id,
                CreationDate = request.CreationDate,
                Summary      = request.Summary,
                Title        = request.Title,
                Image        = request.Image,
                UserId       = request.PostOwner.Id
            };

            ctx.Posts.Add(newPost);
            ctx.SaveChanges();

            foreach (var item in request.Tags)
            {
                PostTag pt = new PostTag {
                    PostId = newPost.Id, TagId = item.Id
                };
                ctx.PostTags.Add(pt);
            }

            ctx.SaveChanges();

            return(_mapper.Map <MPost>(newPost));
        }
Exemplo n.º 2
0
        public Model.Post Insert(PostInsertRequest request)
        {
            var entity = _mapper.Map <Database.Post>(request);

            entity.CreatedAt = DateTime.Now.ToUniversalTime();
            entity.UpdatedAt = DateTime.Now.ToUniversalTime();
            entity.Slug      = CreateSlug(entity.Title);

            //validate does a slug exist already?
            bool valid = SlugValid(entity.Slug);

            if (valid)
            {
                _context.Posts.Add(entity);

                _context.SaveChanges();

                //let's go through the tags and add them
                var tags = request.tagList;
                if (tags != null)
                {
                    if (tags.Count > 0)
                    {
                        foreach (var tag in tags)
                        {
                            //check if tag exists if not add it
                            var check = _context.Tags.Where(t => t.Name == tag).SingleOrDefault();

                            //tag doesn't exist add it first
                            if (check == null)
                            {
                                _context.Tags.Add(new Database.Tag
                                {
                                    Name = tag
                                });
                            }

                            //assign the tag to the new post
                            _context.PostTags.Add(new Database.PostTag
                            {
                                PostId = entity.PostId,
                                TagId  = tag
                            });
                        }
                    }
                }

                _context.SaveChanges();

                return(_mapper.Map <Model.Post>(entity));
            }
            else
            {
                throw new UserException("Post with this title already exists, choose another one");
            }
        }
Exemplo n.º 3
0
        public MPost Insert(PostInsertRequest request)
        {
            MPost newPost = new MPost
            {
                AccountId   = request.AccountId,
                Content     = request.Content,
                PublishDate = request.PublishDate,
                Title       = request.Title
            };

            _posts.Add(newPost);

            return(newPost);
        }
Exemplo n.º 4
0
        public void Add_ValidObjectPassed_ReturnsOk()
        {
            // Arrange
            var testItem = new PostInsertRequest()
            {
                Content     = "Bananas",
                Title       = "Newss",
                PublishDate = DateTime.Now,
                AccountId   = 1
            };
            // Act
            var createdResponse = _controller.Insert(testItem);

            // Assert
            Assert.IsType <OkObjectResult>(createdResponse.Result);
        }
Exemplo n.º 5
0
        public async Task InsertPost()
        {
            try
            {
                if (selectedCategory != null && !string.IsNullOrWhiteSpace(PostSummary) && !string.IsNullOrWhiteSpace(PostTitle))
                {
                    var request = new PostInsertRequest
                    {
                        Category     = selectedCategory,
                        CreationDate = DateTime.Now,
                        PostOwner    = APIService.LoggedUser,
                        Summary      = PostSummary,
                        Title        = PostTitle,
                        Tags         = new List <MTag>()
                    };

                    if (postImage != null)
                    {
                        request.Image = postImage;
                    }

                    foreach (var item in Tags)
                    {
                        if (item.isChecked)
                        {
                            request.Tags.Add(item);
                        }
                    }

                    await _postApiService.Insert <MPost>(request);

                    await Application.Current.MainPage.DisplayAlert("Post", "Created", "Ok");
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Info", "Fill all info", "Ok");
                }
            }
            catch
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Fill all info", "Ok");
            }
        }
Exemplo n.º 6
0
 public MPost Insert(PostInsertRequest request) => _service.Insert(request);
Exemplo n.º 7
0
 public ActionResult <Model.Post> Insert([FromBody] PostInsertRequest request)
 {
     return(_service.Insert(request));
 }
Exemplo n.º 8
0
        public ActionResult <Post> Post([FromBody] PostInsertRequest request)
        {
            var obj = _mapper.Map <Models.Post>(request);

            var check = _context.Posts.Where(w => w.title == request.title).FirstOrDefault();

            if (check != null)
            {
                return(BadRequest());
            }

            obj.createdAt = DateTime.Now;
            obj.updatedAt = DateTime.Now;

            obj.slug = Helpers.Helper.GenerateSlug(request.title);

            _context.Posts.Add(obj);
            _context.SaveChanges();

            int PostID = obj.PostID;

            List <string> tags   = _context.Tags.Select(s => s.title).ToList();
            List <int>    tagsID = _context.Tags.Select(s => s.TagID).ToList();

            for (int i = 0; i < request.tagsList.Count; i++)
            {
                string reqTag = request.tagsList[i];

                bool tagStored = false;

                for (int j = 0; j < tags.Count; j++)
                {
                    string storedTag = tags[j];

                    if (reqTag == storedTag)
                    {
                        _context.PostsTags.Add(
                            new Models.PostTag
                        {
                            PostID = PostID,

                            TagID = tagsID[j]
                        });
                        _context.SaveChanges();
                        tagStored = true;
                        break;
                    }
                }
                if (!tagStored)
                {
                    Models.Tag newTag = new Models.Tag
                    {
                        title = reqTag
                    };
                    _context.Tags.Add(newTag);
                    _context.SaveChanges();

                    _context.PostsTags.Add(
                        new Models.PostTag
                    {
                        PostID = PostID,
                        TagID  = newTag.TagID
                    });
                    _context.SaveChanges();
                }
            }

            var res = _context.Posts.Include("tagList.Tag").Where(w => w.PostID == PostID).FirstOrDefault();

            return(_mapper.Map <Post>(res));
        }