예제 #1
0
        public IActionResult  Post([FromBody] CommodityForCreationDto model)
        {
            try{
                if (model == null)
                {
                    //_logger.LogError("Owner object sent from client is null.");
                    return(BadRequest("Owner object is null"));
                }

                if (!ModelState.IsValid)
                {
                    //_logger.LogError("Invalid owner object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var Entity = _mapper.Map <Commodity>(model);

                _commodityService.Create(Entity);

                var EntityToReturn = _mapper.Map <CommodityDto>(Entity);

                return(CreatedAtRoute("GetById", new { id = EntityToReturn.Id }, EntityToReturn));
            }
            catch (Exception ex)
            {
                //_logger.LogError($"Something went wrong inside CreateOwner action: {ex.Message}");
                return(StatusCode(500, "Internal server error" + ex.ToString()));
            }
        }
예제 #2
0
        public ActionResult Create(CommodityDto model)
        {
            try
            {
                _commodityService.Create(model);

                return(Ok(Response.WriteAsync(JsonConvert.SerializeObject(model,
                                                                          new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }))));
            }
            catch (Exception ex)
            {
                return(BadRequest(Response.WriteAsync(JsonConvert.SerializeObject(ex,
                                                                                  new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }))));
            }
        }
예제 #3
0
        public static void SeedCommodities(ICommodityService commodityService)
        {
            var commodities = new List <Commodity>
            {
                new Commodity {
                    Id = 1, Name = "Onion"
                },

                new Commodity {
                    Id = 2, Name = "Tomato"
                },
            };


            foreach (var commodity in commodities)
            {
                commodityService.Create(commodity).Wait();
            }
        }