コード例 #1
0
        public IHttpActionResult AddTopping(string name)
        {
            try
            {
                Core.Core core    = new Core.Core();
                var       topping = core.GetTopping(name);
                bool      result;

                if (topping == null)
                {
                    result = core.AddTopping(new Topping()
                    {
                        Name = name
                    });
                }
                else
                {
                    throw new DuplicateElementException(name);
                }

                if (result)
                {
                    return(Content(HttpStatusCode.OK, new { Code = (int)HttpStatusCode.OK, Message = "Topping successfully deleted" }));
                }
                else
                {
                    return(Content(HttpStatusCode.Conflict, new { Code = (int)HttpStatusCode.Conflict, Message = "Topping could not be added" }));
                }
            }
            catch (DuplicateElementException dex)
            {
                return(Content(HttpStatusCode.Conflict, new { Code = (int)HttpStatusCode.Conflict, Message = dex.Message }));
            }
            catch (NullReferenceException nex)
            {
                return(Content(HttpStatusCode.NotFound, new { Code = (int)HttpStatusCode.NotFound, Message = nex.Message }));
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.InternalServerError, new { Code = (int)HttpStatusCode.InternalServerError, Message = ex.Message }));
            }
        }
コード例 #2
0
        public IHttpActionResult AddToppingToPizza(string pizzaName, string toppingName)
        {
            try
            {
                Core.Core core  = new Core.Core();
                var       pizza = core.GetPizza(pizzaName);

                if (pizza == null)
                {
                    throw new NullReferenceException("'" + pizzaName + "' Pizza Not Found");
                }
                else
                {
                    var topping = core.GetTopping(toppingName);

                    if (topping == null)
                    {
                        throw new NullReferenceException("'" + toppingName + "' Topping Not Found");
                    }
                    else
                    {
                        bool result = core.AddTopping(topping, pizza);

                        if (!result)
                        {
                            throw new DuplicateElementException("'" + pizzaName + "' Pizza already contains '" + toppingName + "'");
                        }
                    }
                }

                return(Content(HttpStatusCode.Created, new { Code = (int)HttpStatusCode.Created, Message = "Topping successfully added" }));
            }
            catch (DuplicateElementException dex)
            {
                return(Content(HttpStatusCode.Conflict, new { Code = (int)HttpStatusCode.Conflict, Message = dex.Message }));
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.InternalServerError, new { Code = (int)HttpStatusCode.InternalServerError, Message = ex.Message }));
            }
        }