Пример #1
0
        public HttpResponseMessage PostCreateItem(
            ItemModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                if (model.Name == null)
                {
                    throw new ArgumentNullException("name", "The name cannot be null!");
                }

                if (model.ItemCategory == null)
                {
                    throw new ArgumentNullException("category", "The category cannot be null!");
                }

                var context = new StoreContext();
                using (context)
                {
                    var admin = context.Admins.FirstOrDefault(adm => adm.SessionKey == sessionKey);
                    if (admin == null)
                    {
                        throw new InvalidOperationException("You are not admin!");
                    }

                    string categoryNameLower = model.ItemCategory.ToLower();
                    var category             = context.Categories.FirstOrDefault(c => c.Name == categoryNameLower);
                    if (category == null)
                    {
                        category = new ItemCategory()
                        {
                            Name = categoryNameLower
                        };
                    }

                    var item = new Item()
                    {
                        Name         = model.Name,
                        ItemCategory = category,
                        MeleDefense  = model.MeleDefense,
                        MeleAttack   = model.MeleAttack,
                        MagicDefense = model.MagicDefense,
                        MagicAttack  = model.MagicAttack,
                        Description  = model.Description,
                        ImageUrl     = model.ImageUrl,
                        Equiped      = false,
                        HP           = model.HP,
                        MP           = model.MP
                    };

                    context.Items.Add(item);
                    context.SaveChanges();

                    var createdItemModel = new CreatedItemModel()
                    {
                        ItemId = item.ItemId,
                        Name   = item.Name
                    };

                    var response =
                        this.Request.CreateResponse(HttpStatusCode.Created, createdItemModel);
                    return(response);
                }
            });

            return(responseMsg);
        }
        public HttpResponseMessage PostCreateItem(
            ItemModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    if (model.Name == null)
                    {
                        throw new ArgumentNullException("name", "The name cannot be null!");
                    }

                    if (model.ItemCategory == null)
                    {
                        throw new ArgumentNullException("category", "The category cannot be null!");
                    }

                    var context = new StoreContext();
                    using (context)
                    {
                        var admin = context.Admins.FirstOrDefault(adm => adm.SessionKey == sessionKey);
                        if (admin == null)
                        {
                            throw new InvalidOperationException("You are not admin!");
                        }

                        string categoryNameLower = model.ItemCategory.ToLower();
                        var category = context.Categories.FirstOrDefault(c => c.Name == categoryNameLower);
                        if (category == null)
                        {
                            category = new ItemCategory()
                            {
                                Name = categoryNameLower
                            };
                        }

                        var item = new Item()
                        {
                            Name = model.Name,
                            ItemCategory = category,
                            MeleDefense = model.MeleDefense,
                            MeleAttack = model.MeleAttack,
                            MagicDefense = model.MagicDefense,
                            MagicAttack = model.MagicAttack,
                            Description = model.Description,
                            ImageUrl = model.ImageUrl,
                            Equiped = false,
                            HP = model.HP,
                            MP = model.MP
                        };

                        context.Items.Add(item);
                        context.SaveChanges();

                        var createdItemModel = new CreatedItemModel()
                        {
                            ItemId = item.ItemId,
                            Name = item.Name
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created, createdItemModel);
                        return response;
                    }
                });

            return responseMsg;
        }