예제 #1
0
        public async Task <ActionResult <Atribute> > PostAtribute(AtributeDto atributeDto)
        {
            Atribute atribute = new Atribute
            {
                Name = atributeDto.Name
            };

            foreach (long ii in atributeDto.ItemIds)
            {
                if (_context.Items.Any(i => i.Id == ii))
                {
                    ItemAtribute temp = new ItemAtribute
                    {
                        Item     = _context.Items.First(i => i.Id == ii),
                        Atribute = atribute
                    };
                    _context.ItemAtributes.Add(temp);
                }
                else
                {
                    return(BadRequest());
                }
            }

            _context.Atributes.Add(atribute);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetAtribute", new { id = atribute.Id }, atribute));
        }
예제 #2
0
        public async Task <IActionResult> PutAtribute(long id, AtributeDto atributeDto)
        {
            if (id != atributeDto.Id)
            {
                return(BadRequest());
            }

            var atribute = await _context.Atributes.FindAsync(id);

            atribute.Name = atributeDto.Name;
            foreach (long ii in atributeDto.ItemIds)
            {
                if (_context.Items.Any(i => i.Id == ii))
                {
                    if (!_context.ItemAtributes.Any(ia =>
                                                    ia.ItemId == ii &&
                                                    ia.AtributeId == atribute.Id))
                    {
                        ItemAtribute temp = new ItemAtribute
                        {
                            Item     = _context.Items.First(i => i.Id == ii),
                            Atribute = atribute
                        };
                        _context.ItemAtributes.Add(temp);
                    }
                }
                else
                {
                    return(BadRequest());
                }
            }
            _context.Entry(atribute).State = EntityState.Modified;

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

            return(NoContent());
        }