Exemplo n.º 1
0
        public async Task <ActionResult <Level> > PostLevel(PostLevel levelDTO)
        {
            var level = new Level()
            {
                Name        = levelDTO.Name,
                Description = levelDTO.Description,
                Content     = levelDTO.Content,
                Creator     = await _context.User.FirstOrDefaultAsync(u => u.id == levelDTO.CreatorID)
            };

            if (level.Creator == null)
            {
                // User of ScoredOn bestaan niet, bad request.
                return(BadRequest());
            }

            _context.Level.Add(level);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetLevel", new { id = level.ID }, convertGetLevelDTO(level)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutLevel(int id, PostLevel levelDTO)
        {
            if (id != levelDTO.ID)
            {
                return(BadRequest());
            }

            var level = await _context.Level.FirstOrDefaultAsync(l => l.ID == levelDTO.ID);

            level.Name        = levelDTO.Name;
            level.Description = levelDTO.Description;
            level.Content     = levelDTO.Content;
            level.Creator     = await _context.User.FirstOrDefaultAsync(u => u.id == levelDTO.CreatorID);

            if (level.Creator == null)
            {
                // User of ScoredOn bestaan niet, bad request.
                return(BadRequest());
            }

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

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

            return(NoContent());
        }