コード例 #1
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));
            }
        }
コード例 #2
0
        public IHttpActionResult Get()
        {
            try
            {
                var entities = articleService.GetAllArticles().Select(x => ArticleDTO.ToDTO((Article)x));

                if (entities.Any())
                {
                    return(Ok(entities));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #3
0
        // GET: api/Articles/5
        public IHttpActionResult Get(int id)
        {
            try
            {
                var entity = ArticleDTO.ToDTO((Article)articleService.GetById(id));

                if (entity != null)
                {
                    return(Ok(entity));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                //return BadRequest(ex.Message);
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)));
            }
        }