コード例 #1
0
        public async Task <IActionResult> PostTeam([FromBody] TeamReqModel teamReq)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var team = teamReq.ConvertToTeamModel();

            var existingTeam = await TeamExistsAsync(team.Name);

            if (existingTeam)
            {
                return(BadRequest($"Team name: {team.Name} already exists."));
            }

            _context.Team.Add(team);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTeam", new { id = team.Id }, new TeamVm(team)));
        }
コード例 #2
0
        public async Task <IActionResult> PatchTeam([FromRoute] int id, [FromBody] TeamReqModel teamReq)
        {
            // Final request model validation
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var team = teamReq.ConvertToTeamModel();

            team.Id = id;

            var existingTeam = await TeamExistsAsync(team.Name);

            if (existingTeam)
            {
                return(BadRequest($"Team name: {team.Name} already exists."));
            }

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

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

            return(Ok());
        }