예제 #1
0
        public async Task <IHttpActionResult> Post(ItemModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var itemsWithExtId = await Dao.GetItemsByExternalId(model.ExternalId);

            if (itemsWithExtId.Count() != 0)
            {
                return(BadRequest("Item with that ExternalID already exists"));
            }

            var item  = ItemMapper.ModelToEntity(model);
            var newId = await Dao.CreateItem(item);

            if (newId == 0)
            {
                return(InternalServerError());
            }

            var newItem = await Dao.GetItemById(newId);

            return(CreatedAtRoute("GetItemById", new { id = newId }, ItemMapper.EntityToModel(newItem)));
        }
예제 #2
0
        public async Task <IHttpActionResult> GetById(int id)
        {
            var item = await Dao.GetItemById(id);

            if (item is null)
            {
                return(NotFound());
            }
            return(Ok(ItemMapper.EntityToModel(item)));
        }
예제 #3
0
        public async Task <IHttpActionResult> Get(bool onlyMax = false)
        {
            IEnumerable <Item> items;

            if (onlyMax)
            {
                items = await Dao.GetItemsWithMaxPrices();
            }
            else
            {
                items = await Dao.GetItems();
            }

            var modelItems = items.Select(x => ItemMapper.EntityToModel(x));

            return(Ok(modelItems));
        }
예제 #4
0
        public async Task <IHttpActionResult> GetByName(string name, bool onlyMax = false)
        {
            if (onlyMax)
            {
                var item = await Dao.GetItemWithMaxPriceByName(name);

                if (item is null)
                {
                    return(NotFound());
                }
                return(Ok(ItemMapper.EntityToModel(item)));
            }
            else
            {
                var items = await Dao.GetItemsByName(name);

                if (items is null || items.Count() == 0)
                {
                    return(NotFound());
                }
                return(Ok(items.Select(x => ItemMapper.EntityToModel(x))));
            }
        }