Пример #1
0
        public async Task <IActionResult> UpdateSpeaker([FromRoute] int id, [FromBody] ShaunDTO.Speaker input)
        {
            var speaker = await _db.FindAsync <Speaker>(id);

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

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

            speaker.Name    = input.Name;
            speaker.Website = input.Website;
            speaker.Bio     = input.Bio;

            // TODO: Handle exceptions, e.g. concurrency
            await _db.SaveChangesAsync();

            var result = speaker.MapSpeakerResponse();

            return(Ok(result));
        }
Пример #2
0
        public async Task <IActionResult> CreateSpeaker([FromBody] ShaunDTO.Speaker input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var speaker = new Speaker
            {
                Name    = input.Name,
                Website = input.Website,
                Bio     = input.Bio
            };

            _db.Speakers.Add(speaker);
            await _db.SaveChangesAsync();

            var result = speaker.MapSpeakerResponse();

            return(CreatedAtAction(nameof(GetSpeaker), new { id = speaker.ID }, result));
        }