Exemple #1
0
 public IHttpActionResult Get(Guid?id)
 {
     try
     {
         ProductBindigModel model = this.product.GetProduct(id.GetValueOrDefault());
         return(Ok(model));
     }
     catch (BadRequestException bre)
     {
         ModelState.AddModelError(CommonConstants.ErrorKey, bre.Message);
         return(BadRequest(ModelState));
     }
 }
Exemple #2
0
        public IHttpActionResult Post([FromBody] ProductBindigModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                this.product.Create(model.Name, model.Price, model.Mass, model.CategoryId, model.ToppingIds);
                return(Ok(string.Format(CommonConstants.SuccessfullEntityOperation, nameof(Product), CommonConstants.Created)));
            }
            catch (BadRequestException bre)
            {
                ModelState.AddModelError(CommonConstants.ErrorKey, bre.Message);
                return(BadRequest(ModelState));
            }
        }
        public ProductBindigModel GetProduct(Guid id)
        {
            ProductBindigModel model = Database
                                       .Products
                                       .Where(p => p.Id == id)
                                       .Select(p => new ProductBindigModel
            {
                Name       = p.Name,
                Price      = p.Price,
                Mass       = p.Mass,
                CategoryId = p.CategoryId,
                ToppingIds = p.Toppings.Select(t => t.ToppingId)
            })
                                       .FirstOrDefault();

            if (model == null)
            {
                throw new NotExistingEntryException(string.Format(CommonConstants.NotExistingEntry, nameof(Product)));
            }

            return(model);
        }