Esempio n. 1
0
        public async Task <List <DtoArticle> > GetArticlesByAuthorId(int id)
        {
            var dtos = new List <DtoArticle>();

            var articles = await pc.Article.Where(a => a.AuthorId.ToString().Contains(id.ToString())).ToListAsync();

            foreach (var article in articles)
            {
                var intid = (int)article.AuthorId;

                var dtoauthor = await _daoAuthor.Find(intid);

                var dto = new DtoArticle
                {
                    ArticleId  = article.ArticleId,
                    AuthorId   = article.AuthorId,
                    AuthorName = dtoauthor.LastName + ", " + dtoauthor.FirstName,
                    Title      = article.Title,
                    Body       = article.Body
                };

                dtos.Add(dto);
            }

            return(dtos);
        }
Esempio n. 2
0
        public List <DtoArticle> GetArticlesByAuthorId(int id)
        {
            var dtoarticles = new List <DtoArticle>();

            using (var client = new HttpClient())
            {
                var uri = new Uri("http://localhost/WebAPI/api/article/GetArticlesByAuthorId?id=" + id);

                var response = client.GetAsync(uri).Result;

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception(response.ToString());
                }

                var responseContent = response.Content;
                var responseString  = responseContent.ReadAsStringAsync().Result;

                dynamic articles = JArray.Parse(responseString) as JArray;

                foreach (var obj in articles)
                {
                    DtoArticle dto = obj.ToObject <DtoArticle>();

                    dtoarticles.Add(dto);
                }
            }

            return(dtoarticles);
        }
Esempio n. 3
0
        public void Add(ArticleVM.Article article)
        {
            var dto = new DtoArticle
            {
                AuthorId = (int)article.AuthorId,
                Title    = article.Title,
                Body     = article.Body
            };

            svc.Add(dto);
        }
Esempio n. 4
0
        public async Task Add(DtoArticle dto)
        {
            var article = new Models.Article
            {
                AuthorId = dto.AuthorId,
                Title    = dto.Title,
                Body     = dto.Body
            };

            pc.Article.Add(article);
            await pc.SaveChangesAsync();
        }
Esempio n. 5
0
        public void Add(DtoArticle dto)
        {
            var article = new Models.Article
            {
                AuthorId = dto.AuthorId,
                Title    = dto.Title,
                Body     = dto.Body
            };

            pc.Article.Add(article);
            pc.SaveChanges();
        }
Esempio n. 6
0
        public void Update(ArticleVM.Article article)
        {
            var dto = new DtoArticle
            {
                ArticleId = article.ArticleId,
                AuthorId  = article.AuthorId,
                Title     = article.Title,
                Body      = article.Body
            };

            svc.Update(dto);
        }
Esempio n. 7
0
        public async Task Update(DtoArticle dto)
        {
            var article = new Article
            {
                ArticleId = dto.ArticleId,
                AuthorId  = dto.AuthorId,
                Title     = dto.Title,
                Body      = dto.Body
            };

            pc.Entry(article).State = EntityState.Modified;
            await pc.SaveChangesAsync();
        }
Esempio n. 8
0
        public void Update(DtoArticle dto)
        {
            var article = new Article
            {
                ArticleId = dto.ArticleId,
                AuthorId  = dto.AuthorId,
                Title     = dto.Title,
                Body      = dto.Body
            };

            pc.Entry(article).State = EntityState.Modified;
            pc.SaveChanges(false);
        }
Esempio n. 9
0
        public async Task <DtoArticle> Find(int id)
        {
            var dto = new DtoArticle();

            var article = await pc.Article.FindAsync(id);

            if (article != null)
            {
                dto.ArticleId = article.ArticleId;
                dto.AuthorId  = article.AuthorId;
                dto.Title     = article.Title;
                dto.Body      = article.Body;
            }
            else
            {
                throw new Exception($"Article with ID = {id} was not found.");
            }

            return(dto);
        }
Esempio n. 10
0
        public DtoArticle Find(int id)
        {
            var dto = new DtoArticle();

            var article = pc.Article.AsNoTracking().SingleOrDefault(a => a.ArticleId == id);

            if (article != null)
            {
                dto.ArticleId = article.ArticleId;
                dto.AuthorId  = article.AuthorId;
                dto.Title     = article.Title;
                dto.Body      = article.Body;
            }
            else
            {
                throw new Exception($"Article with ID = {id} was not found.");
            }

            return(dto);
        }
Esempio n. 11
0
        public void Add(DtoArticle dto)
        {
            using (var client = new HttpClient {
                BaseAddress = new Uri("http://localhost")
            })
            {
                string serailizeddto = JsonConvert.SerializeObject(dto);

                var inputMessage = new HttpRequestMessage
                {
                    Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json")
                };

                inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage message =
                    client.PostAsync("WebAPI/api/article/Add", inputMessage.Content).Result;

                if (!message.IsSuccessStatusCode)
                {
                    throw new Exception(message.ToString());
                }
            }
        }
 public async Task Update(DtoArticle dto)
 {
     await dao.Update(dto);
 }
 public async Task Add(DtoArticle dto)
 {
     await dao.Add(dto);
 }
Esempio n. 14
0
        public DtoArticle GetById(int id)
        {
            int    nextId            = 0;
            string nextTitle         = "";
            string nextLinkTitle     = "";
            int    previousId        = 0;
            string previousTitle     = "";
            string previousLinkTitle = "";

            var previous = dc.Articles.Where(c => c.Id < id).OrderByDescending(c => c.Id).Take(1).FirstOrDefault();

            previousId        = previous == null ? 0 : previous.Id;
            previousLinkTitle = previous == null ? "" : previous.Title.LinkReplace();
            previousTitle     = previous == null ? "" : previous.Title;

            var next = dc.Articles.Where(c => c.Id > id).Take(1).FirstOrDefault();

            nextId        = next == null ? 0 : next.Id;
            nextLinkTitle = next == null ? "" : next.Title.LinkReplace();
            nextTitle     = next == null ? "" : next.Title;

            var data = (from a in dc.Articles join c in dc.Categories on a.CategoryId equals c.Id where a.Id == id select new {
                Id = a.Id,
                CategoryId = c.Id,
                CategoryName = c.Name,
                Introduction = a.Introduction,
                Title = a.Title,
                Img = a.Img,
                Body = a.Body,
                Type = a.Type,
                Date = a.CreatedDate,
                ArticleTags = a.ArticleTags
            }).FirstOrDefault();

            var article = new DtoArticle();

            if (article != null)
            {
                article.Id               = data.Id;
                article.CategoryId       = data.CategoryId;
                article.CategoryName     = data.CategoryName;
                article.CategoryLinkName = data.CategoryName.LinkReplace();
                article.Introduction     = data.Introduction;
                article.Title            = data.Title;
                article.Img              = "data:image/png;base64," + Convert.ToBase64String(data.Img);
                article.Body             = data.Body;
                article.Type             = data.Type;
                article.CreatedDate      = data.Date;
                article.Tags             = dc.Tags.Where(c => data.ArticleTags.Any(t => t.TagId == c.Id) && c.Active == true).Select(c => new DtoTag {
                    Id       = c.Id,
                    Name     = c.Name,
                    LinkName = c.Name.LinkReplace(),
                    Active   = c.Active
                }).ToList();
                article.NextId            = nextId;
                article.NextTitle         = nextTitle;
                article.NextLinkTitle     = nextLinkTitle;
                article.PreviousId        = previousId;
                article.PreviousTitle     = previousTitle;
                article.PreviousLinkTitle = previousLinkTitle;
            }

            return(article);
        }