Пример #1
0
        public JsonResult AddOrUpdate(PostsDTO model)
        {
            model.Status = true;
            var status = false;

            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
            }
            if (ModelState.IsValid)
            {
                Post posts = new Post();
                posts = AutoMapper.Mapper.Map <Post>(model);
                if (model.ID == 0)
                {
                    status = PostService.Add(posts);
                }
                else
                {
                    status = PostService.Update(posts);
                }
                PostService.Save();
            }
            return(Json(status, JsonRequestBehavior.AllowGet));
        }
Пример #2
0
        public void DeletePostsService(PostsDTO entity)
        {
            var posts = postsRepository.GetAll().Where(x => x.PostId == entity.PostId).FirstOrDefault();

            posts.PostCategoryId  = entity.PostCategoryId;
            posts.PostTitle       = entity.PostTitle;
            posts.PostDiscription = entity.PostDiscription;
            posts.PostPhoto       = entity.PostPhoto;

            postsRepository.Delete(posts);
        }
Пример #3
0
        public void AddPostsService(PostsDTO entity)
        {
            Posts posts = new Posts
            {
                PostCategoryId  = entity.PostCategoryId,
                PostTitle       = entity.PostTitle,
                PostDiscription = entity.PostDiscription,
                PostPhoto       = entity.PostPhoto
            };

            postsRepository.Add(posts);
        }
Пример #4
0
        public async Task <int> UpdatePost(PostsDTO postsDTO)
        {
            try
            {
                int result = await _postsRepository.UpdatePost(_mappingService._mapper.Map <Posts>(postsDTO));

                LogInformation($"Successfully updated a post : PostId { postsDTO.PostId} :");
                return(result);
            }
            catch (Exception ex)
            {
                LogError($"Failed to update a post : PostId { postsDTO.PostId} :", ex);
                return(1);
            }
        }
Пример #5
0
        public async Task <int> CreatePost(PostsDTO postsDTO)
        {
            try
            {
                int resultId = await _postsRepository.CreatePost(_mappingService._mapper.Map <Posts>(postsDTO));

                LogInformation($"Successfully created a new Post : PostID {postsDTO.PostId}, Title {postsDTO.Title} :");
                return(resultId);
            }
            catch (Exception ex)
            {
                LogError($"Failed to create a new post : PostID {postsDTO.PostId}, Title {postsDTO.Title} :", ex);
                return(1);
            }
        }
Пример #6
0
 public PostsDTO CreatePost(PostsDTO post)
 {
     try
     {
         var client   = new MongoClient(connectionString);
         var db       = client.GetDatabase("Social_Network");
         var posts    = db.GetCollection <PostsDTO>("Posts");
         var count_id = posts.CountDocuments(p => p.Post_Id > 0);
         post.Post_Id = (int)count_id + 1;
         posts.InsertOne(post);
         return(post);
     }
     catch (Exception exp)
     {
         throw exp;
     }
 }
Пример #7
0
 public async Task <PostsDTO> GetPostById(int postId)
 {
     if (postId == 0)
     {
         return(null);
     }
     try
     {
         PostsDTO post = _mappingService._mapper.Map <PostsDTO>(await _postsRepository.GetPostById(postId));
         LogInformation($"Successfully fetched a post : postId {post.PostId}, Title {post.Title} :");
         return(post);
     }
     catch (Exception ex)
     {
         LogError($"Failed to fetch a post : postId {postId}", ex);
         return(null);
     }
 }
 public ActionResult <IEnumerable <Post> > GetPosts(string id)
 {
     try
     {
         List <Friend>        clientsList = repository.Friend.FindByCondition(u => u.client_Id == id && u.isUnfriend == false).ToList();
         List <Friend>        friendsList = repository.Friend.FindByCondition(u => u.friend_Id == id && u.isUnfriend == false).ToList();
         IEnumerable <Friend> finalList   = clientsList.Union(friendsList);
         List <Post>          posts       = new List <Post>();
         foreach (var item in clientsList)
         {
             List <Post> friendPosts1 = repository.Post.FindByCondition(u => u.user_Id == item.friend_Id.ToString() && u.isDeleted == false).ToList();
             foreach (var post in friendPosts1)
             {
                 posts.Add(post);
             }
         }
         foreach (var item in friendsList)
         {
             List <Post> friendPosts2 = repository.Post.FindByCondition(u => u.user_Id == item.friend_Id.ToString() && u.isDeleted == false).ToList();
             foreach (var post in friendPosts2)
             {
                 posts.Add(post);
             }
         }
         PostsDTO dto = new PostsDTO()
         {
             success = true,
             data    = mapper.Map <List <PostDTO> >(posts)
         };
         return(Ok(dto));
     }
     catch (Exception ex)
     {
         logger.Error(ex.ToString());
         logger.Warning(ex.StackTrace);
         DTO dto = new DTO()
         {
             success = false,
             message = "An Error has occured"
         };
         return(Ok(dto));
     }
 }
Пример #9
0
        public async Task <HttpResponseMessage> GetPostById(int postId)
        {
            // START TOKEN VALIDATION
            string token = Request.Headers.GetValues("auth_token").FirstOrDefault().ToString();

            if (String.IsNullOrEmpty(token))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Auth token is not valid!"));
            }
            if (_db.AuthenticationTokens.Where(x => x.Token == token).FirstOrDefault().IsDeleted == true)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Auth token is not valid!"));
            }
            TimeSpan difference = DateTime.Now - _db.AuthenticationTokens.Where(x => x.Token == token).FirstOrDefault().TokenDate;

            if (difference.TotalHours > 3)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Auth token is not valid!"));
            }
            // END TOKEN VALIDATION



            Posts foundPost = await _db.Posts.Where(x => x.PostID == postId).Include(x => x.Users).FirstOrDefaultAsync();

            if (foundPost == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Post not found!"));
            }
            PostsDTO forReturn = new PostsDTO
            {
                Date          = foundPost.PostDate.ToShortDateString(),
                PhotoPath     = foundPost.PhotoPath,
                PostID        = foundPost.PostID,
                Text          = foundPost.PostText,
                Title         = foundPost.Title,
                User          = foundPost.Users.Username,
                UserPhotoPath = foundPost.Users.ProfilePhotoPath
            };

            return(Request.CreateResponse(HttpStatusCode.OK, forReturn));
        }
Пример #10
0
        public async Task <ActionResult <PostsDTO> > GetAllPosts(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("model state is not valid"));
            }

            _context.Add(post);

            var success = await _context.SaveChangesAsync();

            var result = new PostsDTO()
            {
                PostId    = post.PostId,
                Message   = post.Message,
                CreatedAt = post.CreatedAt
            };

            return(Ok("post successfully created"));
        }
Пример #11
0
        public PostsDTO UpdatePost(PostsDTO post)
        {
            try
            {
                var client = new MongoClient(connectionString);
                var db     = client.GetDatabase("Social_Network");
                var posts  = db.GetCollection <PostsDTO>("Posts");

                var UpdateFilter = Builders <PostsDTO> .Update.Set("Post_Id", post.Post_Id); // ?

                if (post.Author_Id != 0)
                {
                    UpdateFilter = UpdateFilter.Set("Author_Id", post.Author_Id);
                }
                if (post.Title != null)
                {
                    UpdateFilter = UpdateFilter.Set("Title", post.Title);
                }
                if (post.Body != null)
                {
                    UpdateFilter = UpdateFilter.Set("Body", post.Body);
                }
                if (post.Tags != null)
                {
                    UpdateFilter = UpdateFilter.Set("Tags", post.Tags);
                }

                UpdateFilter = UpdateFilter.Set("Modify", DateTime.Now);


                posts.UpdateOne(g => g.Post_Id == post.Post_Id, UpdateFilter);
                var res = posts.Find(p => p.Post_Id == post.Post_Id).Single();
                return(res);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
Пример #12
0
        public PostsDTO GetBlogposts(string tag = " ")
        {
            var             count   = 0;
            List <Blogpost> results = new List <Blogpost>();

            if (tag == " ")
            {
                results = _context.Blogposts.OrderByDescending(x => x.CreatedAt).ToList();
            }
            else
            {
                results = _context.Blogposts.Where(x => x.TagList.ToList().Contains(tag)).OrderByDescending(x => x.CreatedAt).ToList();
            }
            PostsDTO result = new PostsDTO();

            result.BlogPosts = new List <BlogpostDTO>();
            foreach (var item in results)
            {
                count++;
                BlogpostDTO blogpost = new BlogpostDTO();
                blogpost.Body      = item.Body;
                blogpost.CreatedAt = item.CreatedAt != null?item.CreatedAt.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") : "Not available";

                blogpost.UpdatedAt = item.UpdatedAt != null?item.UpdatedAt.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ") : "Not available";

                blogpost.Description = item.Description;
                blogpost.Slug        = item.Slug;
                blogpost.Title       = item.Title;
                blogpost.TagList     = item.TagList;

                result.BlogPosts.Add(blogpost);
            }
            result.PostsCount = count;

            return(result);
        }
Пример #13
0
 public IActionResult AddNewPost(PostsDTO entity)
 {
     postsServices.AddPostsService(entity);
     return(Redirect("/Post"));
 }
Пример #14
0
 public IActionResult UpdatePost(PostsDTO entity)
 {
     postsServices.UpdatePostsService(entity);
     return(Redirect("/Post"));
 }