Ejemplo n.º 1
0
        public async Task <string> Create(ColorCreateRequest request)
        {
            var color = new productColor()
            {
                idColor   = request.IdColor,
                colorName = request.Name,
            };

            _context.productColor.Add(color);
            await _context.SaveChangesAsync();

            return(color.idColor);
        }
        public async Task <IActionResult> ProdectPost(int Id, [FromForm] productViewUpdata model)
        {
            var user = await userManager.FindByIdAsync(User.FindFirst("Id")?.Value);

            if (user == null)
            {
                return(Unauthorized());
            }
            if (await userManager.IsInRoleAsync(user, "Sealler") && user?.Confirmed == Confirmed.approved && !user.Block)
            {
                try
                {
                    var forms         = new List <FormView>();
                    var Colors        = new List <ColorView>();
                    var ImgPathupData = new List <string>();
                    ; if (model.Forms != null)
                    {
                        forms = JsonConvert.DeserializeObject <List <FormView> >(model.Forms);
                    }

                    if (model.Colors != null)
                    {
                        Colors = JsonConvert.DeserializeObject <List <ColorView> >(model.Colors);
                    }
                    if (model.ImgPathupData != null)
                    {
                        ImgPathupData = JsonConvert.DeserializeObject <List <string> >(model.ImgPathupData);
                    }

                    var product = _contect.products
                                  .Include(i => i.Colors)

                                  .Include(i => i.Form)

                                  .Include(i => i.Img)
                                  .SingleOrDefault(i => i.Id == Id && i.SeallerId == user.Id && i.Delete == false);
                    if (product == null)
                    {
                        return(NotFound());
                    }

                    product.Name        = model.Name;
                    product.AName       = model.AName;
                    product.description = model.description;
                    product.Stock       = model.Stock;
                    product.price       = model.price;

                    product.StockCount = model.Stock == true ? model.StockCount : 0;
                    product.Timespent  = model.Timespent;
                    //  _contect.SaveChanges();
                    if (forms != null)
                    {
                        foreach (var item in forms)
                        {
                            var Form = product.Form.SingleOrDefault(i => i.FormId == item.FormId);
                            if (Form != null)
                            {
                                Form.value = item.Value;
                            }
                            else
                            {
                                Form = new productForm()
                                {
                                    value = item.Value, ProductId = Id, FormId = item.FormId
                                };
                                product.Form.Add(Form);
                            }
                            //_contect.SaveChanges();
                        }
                    }

                    if (Colors != null)
                    {
                        foreach (var item in Colors)
                        {
                            var Color = product.Colors.SingleOrDefault(i => i.Id == item.Id);
                            if (Color != null)
                            {
                                Color.AColor = item.AColor;
                                Color.Code   = item.Code;
                                Color.Color  = item.Color;
                            }
                            else
                            {
                                Color = new productColor()
                                {
                                    productId = Id, AColor = item.AColor, Color = item.Color, Code = item.Code
                                };
                                product.Colors.Add(Color);
                            }
                            // _contect.SaveChanges();
                        }
                    }

                    List <IFormFile> Img = new List <IFormFile>();
                    Img.Add(model.Img1);
                    Img.Add(model.Img2);
                    Img.Add(model.Img3);
                    Img.Add(model.Img4);
                    Img.Add(model.Img5);
                    var productIMg = ImgMapForm.ImgList(wwwroot, Img);
                    for (int i = 0; i < productIMg.Count; i++)
                    {
                        if (ImgPathupData.Count - 1 >= i)
                        {
                            var OldImg       = ImgPathupData[i].Substring(imgProdectPath.Length);
                            var productIMgup = product.Img.SingleOrDefault(o => o.img == OldImg);
                            if (productIMgup != null)
                            {
                                productIMgup.img = productIMg[i];
                            }
                        }
                        else
                        {
                            var productIMgup = new productIMg()
                            {
                                productId = Id, img = productIMg[i].ToString()
                            };
                            product.Img.Add(productIMgup);
                        }
                        // _contect.SaveChanges();
                    }

                    _contect.SaveChanges();

                    foreach (var item in ImgPathupData)
                    {
                        var fileNameold = wwwroot + item.Substring(imgProdectPath.Length);

                        if (System.IO.File.Exists(fileNameold))
                        {
                            System.IO.File.Delete(fileNameold);
                        }
                    }


                    return(Ok(new
                    {
                        product.Id,
                        product.AName,
                        product.Name,
                        product.description,
                        product.Date,
                        product.price,
                        product.StockCount,
                        product.Stock,
                        product.Timespent,
                        product.SeallerId,

                        Forms = product.Form.Select(i => new { i.FormId, i.value }).ToList(),
                        Colors = product.Colors.Select(i => new { i.Id, i.AColor, i.Color, i.Code }).ToList(),
                        imgs = product.Img.Select(i => new { i.Id, img = imgProdectPath + i.img }).ToList(),
                    }));
                }
                catch (Exception e) { return(BadRequest(e.InnerException.ToString())); }
            }
            else
            {
                return(Unauthorized());
            }
        }