示例#1
0
 public ICollection <Article> GetArticles()
 {
     if (Request.Headers.Referrer != null && Request.Headers.Referrer.ToString().Contains(Config.SERVER))
     {
         var articles = db.Articles.Include(c => c.Stats)
                        .Include(c => c.Materials.Select(y => y.Stats))
                        .Include(c => c.Materials.Select(y => y.Type))
                        .OrderBy(c => c.Date).ToList();
         articles.Reverse();
         return(JsonHelper.ConvertArticles(articles));
     }
     return(null);
 }
示例#2
0
        public async Task <IHttpActionResult> PostArticle([FromBody] Article article)
        {
            if (Request.Headers.Referrer != null && Request.Headers.Referrer.ToString().Contains(Config.SERVER))
            {
                article.Date = DateTime.Now;
                db.Articles.Add(article);
                await db.SaveChangesAsync();

                var art = db.Articles.Include(c => c.Stats)
                          .Include(c => c.Materials.Select(y => y.Stats))
                          .Include(c => c.Materials.Select(y => y.Type)).Where(c => c.Title == article.Title).FirstOrDefault();
                return(Ok(JsonHelper.ConvertArticles(new List <Article>()
                {
                    art
                })));
            }
            return(BadRequest());
        }
示例#3
0
        public async Task <IHttpActionResult> PutArticle(int id, Article article)
        {
            if (Request.Headers.Referrer != null && Request.Headers.Referrer.ToString().Contains(Config.SERVER))
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (id != article.Id)
                {
                    return(BadRequest());
                }

                var articleDb = await db.Articles.Where(c => c.Id == id).FirstOrDefaultAsync();

                articleDb.Title = article.Title;
                articleDb.Body  = article.Body;
                await db.SaveChangesAsync();

                try
                {
                    await db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ArticleExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(Ok(JsonHelper.ConvertArticles(new List <Article>()
                {
                    articleDb
                })));
            }
            return(BadRequest());
        }
示例#4
0
        public async Task <IHttpActionResult> GetArticle(int id)
        {
            if (Request.Headers.Referrer != null && Request.Headers.Referrer.ToString().Contains(Config.SERVER))
            {
                Article article = await db.Articles.Include(c => c.Stats)
                                  .Include(c => c.Materials.Select(y => y.Stats))
                                  .Include(c => c.Materials.Select(y => y.Type))
                                  .Where(c => c.Id == id).FirstOrDefaultAsync();

                if (article == null)
                {
                    return(NotFound());
                }
                return(Ok(JsonHelper.ConvertArticles(new List <Article>()
                {
                    article
                })));
            }
            return(BadRequest());
        }