public async Task <ActionResult <Pub> > PostPub()
        {
            try
            {
                Guid   id       = Guid.NewGuid();
                var    file     = Request.Form.Files[0];
                string path     = "wwwroot/uploads/PubImages/" + id;
                string fullPath = "";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                if (file.Length > 0)
                {
                    fullPath = Path.Combine(path, file.FileName);
                    var stream = new FileStream(fullPath, FileMode.Create);
                    await file.CopyToAsync(stream);
                }
                _context.PubsImages.Add(new Pub()
                {
                    IdIPubImage = id, PubImageName = "Uploads/PubImages/" + id + "/" + file.FileName
                });
                await _context.SaveChangesAsync();

                return(Ok(_context));
            }
            catch (Exception e)
            {
                return(Ok("fails execption" + e));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutProduit(Guid id, Produit produit)
        {
            if (id != produit.IdProd)
            {
                return(BadRequest());
            }



            _context.Entry(produit).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProduitExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutCategorie(Guid id, Categorie categorie)
        {
            if (id != categorie.IdCat)
            {
                return(BadRequest());
            }

            _context.Entry(categorie).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategorieExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a Produkt
        /// </summary>
        /// <param name="newProdukt"></param>
        /// <returns></returns>
        public async Task <ProduktDto> Create(ProduktDto newProdukt)
        {
            Produkt produkt = new Produkt {
                Navn = newProdukt.Navn, Pris = newProdukt.Pris, KategoriId = newProdukt.KategoriId, ProducentId = newProdukt.ProducentId
            };

            _context.Produkter.Add(produkt);
            await _context.SaveChangesAsync();

            return(newProdukt);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Upload(Guid id)
        {
            try
            {
                if (!Request.HasFormContentType)
                {
                    throw new InvalidOperationException("Incorrect Content-Type: " + Request.ContentType);
                }

                var    file     = Request.Form.Files[0];
                string path     = "wwwroot/uploads/" + id;
                string fullPath = "";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                if (file.Length > 0)
                {
                    fullPath = Path.Combine(path, file.FileName);
                    var stream = new FileStream(fullPath, FileMode.Create);
                    await file.CopyToAsync(stream);

                    stream.Close();
                }
                _context.Images.Add(new Image()
                {
                    ImageName = "Uploads/" + id + "/" + file.FileName, ProduitId = id
                });
                await _context.SaveChangesAsync();

                var prod = await _context.Produits.FindAsync(id);

                prod.FrontImg = prod.Images.FirstOrDefault <Image>().ImageName;
                _context.Entry(prod).State = EntityState.Modified;
                await _context.SaveChangesAsync();



                return(Ok(_context));
            }
            catch (Exception e)
            {
                return(Ok("fails execption" + e));
            }
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Description,Price")] Product product, IFormFile Image)
        {
            if (ModelState.IsValid)
            {
                if (Image != null)
                {
                    if (Image.Length > 0)
                    {
                        using (var ms = new MemoryStream())
                        {
                            Image.CopyTo(ms);
                            product.Image = ms.ToArray();
                        }
                    }
                }
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Ejemplo n.º 7
0
 public async Task Save()
 {
     await _db.SaveChangesAsync();
 }