public IHttpActionResult PutArticle(int id, ArticleDTO article)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                if (id != article.Id)
                {
                    return(BadRequest());
                }

                Repository.Update(article.ToModel());
                Article updated = Repository.GetByID(id);
                return(Ok(updated.DTO));
            }
            catch (NullReferenceException nre)
            {
                return(NotFound());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
예제 #2
0
        public IHttpActionResult Update(int id, [FromBody] ArticleDTO article)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }
                if (article == null)
                {
                    return(BadRequest(nameof(article)));
                }

                var domainArticle = ArticleDTO.ToModel(article);
                domainArticle.Id = id;
                var updatedEntity = articleService.Update(domainArticle);

                if (updatedEntity != null)
                {
                    return(Ok(updatedEntity));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
예제 #3
0
 public IHttpActionResult SoftDelete([FromBody] IEnumerable <ArticleDTO> articles)
 {
     try
     {
         var status = articleService.SoftDeleteRange(articles.Select(x => ArticleDTO.ToModel(x)));
         if (status)
         {
             return(Ok());
         }
         else
         {
             return(NotFound());
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
예제 #4
0
        public IHttpActionResult Create([FromBody] ArticleDTO article)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }
                var domainArticle = ArticleDTO.ToModel(article);
                var addedArticle  = articleService.Add(domainArticle);

                var addedArticleDTO = ArticleDTO.ToDTO((Article)addedArticle);

                return(CreatedAtRoute("DefaultApi", new { id = addedArticle.Id }, addedArticleDTO));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
 public IHttpActionResult PostArticle(ArticleDTO article)
 {
     //ModelState.Remove("Model");
     try
     {
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         var        inserted = Repository.Insert(article.ToModel());
         ArticleDTO dto      = inserted.DTO;
         return(Ok(dto));
     }
     catch (NullReferenceException nre)
     {
         return(NotFound());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }