コード例 #1
0
        // POST: api/Dispensaries
        public async Task <HttpResponseMessage> Post([FromBody] DispensaryProductModel product)
        {
            try
            {
                foreach (var v in product.DispensaryProductVariants)
                {
                    foreach (var p in v.VariantPricing)
                    {
                        if (v.IsPricedByWeight)
                        {
                            var oz = p.Weight / 28M;
                            if (1 / 28M == oz)
                            {
                                p.DisplayWeight = "Gram";
                            }
                            else if (.125M == oz)
                            {
                                p.DisplayWeight = "Eighth";
                            }
                            else if (.25M == oz)
                            {
                                p.DisplayWeight = "Quarter";
                            }
                            else if (.5M == oz)
                            {
                                p.DisplayWeight = "Half";
                            }
                            else if (1M == oz)
                            {
                                p.DisplayWeight = "Ounce";
                            }
                            else
                            {
                                p.DisplayWeight = String.Format("{0}g", p.Weight);
                            }
                        }
                        else
                        {
                            if (1M == p.Weight)
                            {
                                p.DisplayWeight = "Each";
                            }
                            else
                            {
                                p.DisplayWeight = String.Format("{0}-pack", p.Weight);
                            }
                        }
                    }
                }
                var entity = Mapper.Map <DispensaryProduct>(product);

                List <int> ids;
                foreach (DispensaryProductVariant variant in entity.DispensaryProductVariants)
                {
                    ids            = variant.Photos.Select(p => p.Id).ToList();
                    variant.Photos = HGContext.Files.Where(i => ids.Contains(i.Id)).ToList();
                }

                ids            = entity.Effects.Select(e => e.Id).ToList();
                entity.Effects =
                    HGContext.Effects.Where(e => ids.Contains(e.Id)).ToList();
                ids             = entity.Symptoms.Select(e => e.Id).ToList();
                entity.Symptoms =
                    HGContext.Symptoms.Where(e => ids.Contains(e.Id)).ToList();


                entity.YouTubeVideoUrl = YouTubeVideoHelper.TransformYouTubeVideo(entity.YouTubeVideoUrl);

                entity.ProductCategory =
                    HGContext.ProductCategories.FirstOrDefault(cat => cat.Id == product.ProductCategoryId);

                HGContext.DispensaryProducts.Add(entity);
                var id = await HGContext.SaveChangesAsync();

                if (id > 0)
                {
                    return(Request.CreateResponse(Mapper.Map <DispensaryProductModel>(HGContext.DispensaryProducts.FirstOrDefault(x => x.Id == id))));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, product));
                }
            }
            catch (Exception e)
            {
                _logger.Error("Dispensaries.Post", e);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #2
0
        public async Task <HttpResponseMessage> Update([FromBody] DispensaryProductModel model)
        {
            try
            {
                foreach (var v in model.DispensaryProductVariants)
                {
                    foreach (var p in v.VariantPricing)
                    {
                        if (v.IsPricedByWeight)
                        {
                            var oz = p.Weight / 28M;
                            if (1 / 28M == oz)
                            {
                                p.DisplayWeight = "Gram";
                            }
                            else if (.125M == oz)
                            {
                                p.DisplayWeight = "Eighth";
                            }
                            else if (.25M == oz)
                            {
                                p.DisplayWeight = "Quarter";
                            }
                            else if (.5M == oz)
                            {
                                p.DisplayWeight = "Half";
                            }
                            else if (1M == oz)
                            {
                                p.DisplayWeight = "Ounce";
                            }
                            else
                            {
                                p.DisplayWeight = String.Format("{0}g", p.Weight);
                            }
                        }
                        else
                        {
                            if (1M == p.Weight)
                            {
                                p.DisplayWeight = "Each";
                            }
                            else
                            {
                                p.DisplayWeight = String.Format("{0}-pack", p.Weight);
                            }
                        }
                    }
                }
                var entity =
                    await
                    HGContext.DispensaryProducts.Include(product => product.DispensaryProductVariants)
                    .Include(product => product.DispensaryProductVariants.Select(variant => variant.Photos))
                    .FirstOrDefaultAsync(x => x.Id == model.Id);

                entity.Name            = model.Name;
                entity.Slug            = model.Slug;
                entity.LeaflySlug      = model.LeaflySlug ?? model.Name;
                entity.Description     = model.Description;
                entity.UpdatedAt       = DateTimeOffset.UtcNow;
                entity.YouTubeVideoUrl = YouTubeVideoHelper.TransformYouTubeVideo(model.YouTubeVideoUrl);
                entity.ProductCategory = HGContext.ProductCategories.FirstOrDefault(c => c.Id == model.ProductCategoryId);

                List <int> ids = model.Effects.Select(e => e.Id).ToList();
                entity.Effects.Clear();
                entity.Effects = HGContext.Effects.Where(e => ids.Contains(e.Id)).ToList();

                ids = model.Symptoms.Select(e => e.Id).ToList();
                entity.Symptoms.Clear();
                entity.Symptoms = HGContext.Symptoms.Where(e => ids.Contains(e.Id)).ToList();

                entity.DispensaryProductVariants.ForEach(v => v.IsDeleted = true);

                foreach (DispensaryProductVariantModel variant in model.DispensaryProductVariants)
                {
                    DispensaryProductVariant variantEntity =
                        entity.DispensaryProductVariants.FirstOrDefault(v => v.Id == variant.Id);
                    if (variantEntity != null)
                    {
                        variantEntity.IsDeleted = false;

                        ids = variant.Photos.Select(p => p.Id).ToList();
                        List <File> images =
                            HGContext.Files.Where(i => ids.FirstOrDefault(p => p == i.Id) != 0).ToList();

                        if (variantEntity.Photos == null)
                        {
                            variantEntity.Photos = new List <File>();
                        }

                        variantEntity.Photos.Clear();
                        variantEntity.Photos.AddRange(images);

                        variantEntity.VariantPricing = Mapper.Map <string>(variant.VariantPricing);
                    }
                }

                var statusCode = await HGContext.SaveChangesAsync();

                if (statusCode > 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unable to save product"));
                }
            }
            catch (Exception e)
            {
                _logger.Error("DispensaryProduct.Update", e);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
コード例 #3
0
        public ViewResult Details([FromUri][Required] int id)
        {
            using (var context = new HGContext())
            {
                DispensaryProductModel product = context.DispensaryProducts.AsNoTracking()
                                                 .Include(p => p.DispensaryProductVariants.Select(v => v.Photos))
                                                 .Where(p => !p.IsDeleted && !p.Dispensary.IsDeleted && p.Id == id)
                                                 .Select(item => new DispensaryProductModel()
                {
                    Id          = item.Id,
                    Name        = item.Name,
                    Description = item.Description,
                    Dispensary  = new ThinDispensaryModel()
                    {
                        Name = item.Dispensary.Name,
                        Id   = item.Dispensary.Id,
                        Slug = item.Dispensary.Slug
                    },
                    ProductCategory =
                        new ProductCategoryModel()
                    {
                        Id = item.ProductCategory.Id, Name = item.ProductCategory.Name
                    },
                    DispensaryProductVariants =
                        item.DispensaryProductVariants.Select(v => new DispensaryProductVariantModel()
                    {
                        Id = v.Id,
                        IsMasterVariant = v.IsMasterVariant,
                        DisplayOrder    = v.DisplayOrder,
                        Name            = v.Name,
                        Photos          = v.Photos.Select(p => new FileModel()
                        {
                            Id = p.Id
                        }),
                        IsPricedByWeight   = v.IsPricedByWeight,
                        VariantQuantity    = v.VariantQuantity,
                        VariantPricingJSON = v.VariantPricing
                    }),
                    Effects = item.Effects.Select(e => new EffectModel()
                    {
                        Id = e.Id, Name = e.Name, Description = e.Description
                    }),
                    Symptoms = item.Symptoms.Select(e => new SymptomModel()
                    {
                        Id = e.Id, Name = e.Name, Description = e.Description
                    }),
                    IsAvailable     = item.IsAvailable,
                    IsDiscounted    = item.IsDiscounted,
                    IsPopular       = item.IsPopular,
                    Slug            = item.Slug,
                    YouTubeVideoUrl = item.YouTubeVideoUrl
                }).FirstOrDefault();


                if (product == null)
                {
                    throw new HttpException(404, "Can't find product");
                }

                foreach (DispensaryProductVariantModel variant in product.DispensaryProductVariants)
                {
                    variant.VariantPricing = Mapper.Map <List <VariantPricing> >(variant.VariantPricingJSON);
                }


                return(View(product));
            }
        }