Пример #1
0
        public async Task <IActionResult> Update([FromRoute] long id, [FromBody] Item Item)
        {
            if (Item == null || Item.Id != id)
            {
                return(BadRequest());
            }

            var dbItem = await _context.Items.FirstOrDefaultAsync(d => d.Id == id);

            if (dbItem == null)
            {
                return(NotFound());
            }

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

            if (await TryUpdateModelAsync <Item>(Item))
            {
                await _context.SaveChangesAsync();

                return(CreatedAtRoute("GetItem", new { id = Item.Id }, Item));
            }

            dbItem.Update(Item);

            _context.Items.Update(dbItem);
            _context.SaveChanges();
            return(new NoContentResult());
        }
        public IActionResult Create([FromBody] Category category)
        {
            if (category == null)
            {
                return(BadRequest());
            }
            var laboratory = _context.Laboratories.FirstOrDefault(lab => lab.Id == category.Laboratory.Id);

            if (laboratory == null)
            {
                return(BadRequest("The Laboratory does not exist."));
            }

            category.Laboratory = laboratory;
            _context.Categories.Add(category);
            _context.SaveChanges();

            return(CreatedAtRoute("GetCategory", new { id = category.Id }, category));
        }
Пример #3
0
        public DeviceController(MyLabContext context)
        {
            _context = context;

            if (_context.Devices.Count() == 0)
            {
                _context.Devices.Add(new Device {
                    Title = "Item1"
                });
                _context.SaveChanges();
            }
        }