Beispiel #1
0
        public async Task <PlatosDtoCU> Updatedto(int id, PlatosDtoCU dto)
        {
            var item = await _context.Set <Platos>().FindAsync(id);

            if (item == null)
            {
                return(null);
            }
            item.Nombre    = dto.Nombre;
            item.Precio    = dto.Precio;
            item.Categoria = dto.Categoria;
            item.Personas  = dto.Personas;


            var ingredientes = await _context.IngredientesPlato.Where(a => a.IdPlato == id).ToListAsync();

            foreach (var ing in ingredientes)
            {
                _context.IngredientesPlato.Remove(ing);
                await _context.SaveChangesAsync();
            }

            foreach (var ids in dto.Ingredientes)
            {
                if (await _context.Ingredientes.FindAsync(ids) != null)
                {
                    var ingplato = new IngredientesPlato();
                    ingplato.IdIngrediente = ids;
                    ingplato.IdPlato       = item.Id;
                    await _context.IngredientesPlato.AddAsync(ingplato);

                    await _context.SaveChangesAsync();
                }

                _context.Update(item);
                await _context.SaveChangesAsync();
            }
            return(dto);
        }
Beispiel #2
0
        public async Task <bool> Adddto(PlatosDtoCU entity)
        {
            var item = _mapper.Map <Platos>(entity);

            _context.Set <Platos>().Add(item);
            await _context.SaveChangesAsync();

            foreach (var id in entity.Ingredientes)
            {
                if (await _context.Ingredientes.FindAsync(id) != null)
                {
                    var ingplato = new IngredientesPlato();
                    ingplato.IdIngrediente = id;
                    ingplato.IdPlato       = item.Id;
                    await _context.IngredientesPlato.AddAsync(ingplato);

                    await _context.SaveChangesAsync();
                }
            }


            return(true);
        }