예제 #1
0
        public async Task <IActionResult> PutUnitType([FromRoute] int id, [FromBody] UnitType unitType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != unitType.UnitTypeId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> PostProduct([FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProduct", new { id = product.ProductId }, product));
        }
예제 #3
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product)
        {
            Console.WriteLine("ErrorAQui");
            //JsonConvert.DeserializeObject(product);
            string json = JsonConvert.SerializeObject(product, Formatting.Indented);

            Console.WriteLine(json);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            // _context.Entry(product).State = EntityState.Modified;
            _context.Update(product);
            var productDB = _context.Products.Single(a => a.ProductId == product.ProductId);
            var images    = _context.Image.Where(b => EF.Property <int>(b, "ProductId") == product.ProductId);

            foreach (var image in images)
            {
                productDB.Images.Remove(image);
            }
            //_context.Entry(product).Property(x=> x.Images).IsModified = false;
            _context.Update(product);

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

            return(NoContent());
        }
예제 #4
0
        public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Item).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            Mensagem = $"Item {Item.Nome} salvo com sucesso";

            return(RedirectToPage("./Index"));
        }
예제 #5
0
 public async Task <bool> SaveAll()
 {
     return(await _context.SaveChangesAsync() > 0);  // if is == 0 nothing has been saved and return false
 }
        public async Task <IActionResult> OnPostCreateFileAsync()
        {
            var nameFile = $"{Guid.NewGuid()}.txt";
            var savePath = Path.Combine(_hostEnvironment.WebRootPath, "arquivo", nameFile);

            var newFile = System.IO.File.CreateText(savePath);

            var list = await _context
                       .Itens
                       .ToListAsync()
                       .ConfigureAwait(false);

            var lineFirst = "id".PadRight(5) +
                            "|" +
                            "nome".PadRight(60) +
                            "|" +
                            "precoCusto".PadRight(10) +
                            "|" +
                            "precoVenda".PadRight(10) +
                            "|" +
                            "ncm".PadRight(8) +
                            "|" +
                            "referencia".PadRight(14) +
                            "|" +
                            "dataCriacao".PadRight(19);


            newFile.WriteLine(lineFirst);

            foreach (var item in list)
            {
                var referencia = item.Referencia == null ? "" : item.Referencia;

                var line = $"{item.Id.ToString().PadRight(5)}|"
                           + $"{item.Nome.PadRight(60)}|"
                           + $"{item.PrecoCusto.ToString().PadRight(10)}|"
                           + $"{item.PrecoVenda.ToString().PadRight(10)}|"
                           + $"{item.Ncm.PadRight(8)}|"
                           + $"{referencia.PadRight(14)}|"
                           + $"{item.DataCadastro.ToString().PadRight(19)}";

                newFile.WriteLine(line);

                _context
                .Itens
                .Remove(item);
            }

            newFile.Close();

            await _context
            .SaveChangesAsync()
            .ConfigureAwait(false);

            await CarregarItens()
            .ConfigureAwait(false);

            byte[] stream = System.IO.File.ReadAllBytes(savePath);

            return(File(stream, "text/plain ", "Report.txt"));
        }