コード例 #1
0
        public async Task <HttpResponseMessage> Update([FromBody] ProductModel model)
        {
            try
            {
                var entity =
                    await
                    HGContext.Products.Include(prod => prod.Photos).FirstOrDefaultAsync(x => x.Id == model.Id);

                entity.Name            = model.Name;
                entity.Slug            = model.Slug;
                entity.YouTubeVideoUrl = YouTubeVideoHelper.TransformYouTubeVideo(model.YouTubeVideoUrl);
                entity.LeaflySlug      = model.LeaflySlug ?? model.Name;
                entity.Description     = model.Description;
                entity.UpdatedAt       = DateTimeOffset.UtcNow;
                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();

                ids = model.Photos.Select(p => p.Id).ToList();
                entity.Photos.Clear();
                entity.Photos = HGContext.Files.Where(i => ids.Contains(i.Id)).ToList();

                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));
            }
        }
コード例 #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
        // 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));
            }
        }