Пример #1
0
        public List <DtoPost> GetPostsByUserId(int userId)
        {
            string endpointPost = ConfigurationManager.AppSettings["Posts"];

            List <DtoPost> posts = new List <DtoPost>();
            var            text  = client.DownloadString($"{_urlApi}/{endpointPost}?userId={userId}");
            object         desSerializedObject = _serializer.DeserializeObject(text);

            if (desSerializedObject != null)
            {
                var respuesta = (object[])desSerializedObject;
                foreach (var item in respuesta)
                {
                    var     dataPost = (Dictionary <string, object>)item;
                    DtoPost Post     = new DtoPost
                    {
                        Id       = (int)dataPost["id"],
                        Title    = dataPost["title"].ToString(),
                        UserId   = (int)dataPost["userId"],
                        Body     = dataPost["body"].ToString(),
                        Comments = GetCommentsByCommentId((int)dataPost["id"])
                    };

                    posts.Add(Post);
                }
            }

            return(posts);
        }
        public IActionResult PushPost(DtoPost post)
        {
            int data = 0;

            if (post.Description == null && post.Photo == null)
            {
                data = 0;
            }
            else
            {
                PosM.CreatePost(post);
                data = 1;
            }
            return(Json(data));
        }
Пример #3
0
        /// <summary>
        /// Добавление статьи в БД
        /// </summary>
        /// <param name="dtoPost">ДТО объекта статьи</param>
        public async Task AddToDbAsync(DtoPost dtoPost)
        {
            string guid = Guid.NewGuid().ToString();

            var post = _mapper.Map <Post>(dtoPost);

            post.AuthorExternalId = guid;
            post.OwnerId          = User.Id;
            await _repoPosts.AddPostAsync(post);

            var authors = _mapper.Map <List <Author> >(dtoPost.Authors);
            await _repoAuthors.AddAuthorsRangeAsync(authors.With(guid));

            await UpdatePostsCashAsync();
            await UpdatePostsFiltersCashAsync();
        }
Пример #4
0
        public void CreatePost(DtoPost post)
        {
            Post postentity = new Post();

            postentity.CommentCount = post.CommentCount;
            postentity.Description  = post.Description;
            postentity.LikeCount    = post.LikeCount;
            postentity.Photo        = post.Photo;
            postentity.Replyto      = post.Replyto;
            postentity.UserID       = post.UserID;
            postentity.CreatedDate  = DateTime.Now;
            postentity.isactive     = true;
            postentity.isdelete     = false;

            fbcontext.Post.Add(postentity);
            fbcontext.SaveChanges();
        }
Пример #5
0
        public IHttpActionResult PostMessage([FromBody] DtoPost message)
        {
            //todo pass in time
            DateTime time = DateTime.Now;

            if (message == null || string.IsNullOrWhiteSpace(message.Message))
            {
                return(Unauthorized());
            }

            if (message.Message == "1/0")
            {
                throw new DivideByZeroException(message.Message);
            }

            if (message.Message == "0/0")
            {
                throw new LskExcepiton("Divide by zero: " + message.Message);
            }

            //a test message
            //moon //lsk// game of throne ended //lsk// admin
            //var weekDay = DateTime.Now.DayOfWeek.ToString().Select(c => c.ToString()).ToArray();
            var messageParts = message.Message.Split(new[] { Constants.MessageSeparator }, StringSplitOptions.RemoveEmptyEntries);

            if (messageParts.Length < 3)
            {
                return(Unauthorized());
            }

            //if (!messageParts.First().StartsWith(weekDay[0], StringComparison.OrdinalIgnoreCase))
            //{
            //    return Unauthorized();
            //}

            //if (!messageParts.Last().StartsWith(weekDay[1], StringComparison.OrdinalIgnoreCase))
            //{
            //    return Unauthorized();
            //}

            var content = new List <string>();

            if (!string.IsNullOrWhiteSpace(message.Title))
            {
                content.Add("//title//" + message.Title);
            }

            content.AddRange(messageParts.Skip(1).Take(messageParts.Length - 2));

            if (!string.IsNullOrWhiteSpace(message.Title))
            {
                content.Add("".PadLeft(128, '-') + Environment.NewLine);
            }

            var path      = GetFullTitledMessagePath(time);
            var alternate = GetFullTitledMessageAlterPath(time);

            AppendToFile(path, string.Join(Environment.NewLine, content), alternate);

            return(Json($"Your post{(string.IsNullOrWhiteSpace(message.Title) ? "" : " (" + message.Title.Trim() + ")")} is saved."));

            //await the async method to finish?? to ensure message saved??
            //actual should return: Created("...")
        }