コード例 #1
0
        public async Task <ServiceResponse <List <GetCharacterDto> > > AddCharacter(GetCharacterDto newCharacter)
        {
            var serviceResponse = new ServiceResponse <List <GetCharacterDto> >();
            var character       = _mapper.Map <Character>(newCharacter);

            character.Id = characters.Max(c => c.Id) + 1;
            characters.Add(character);
            serviceResponse.Data = characters.Select(c => _mapper.Map <GetCharacterDto>(c)).ToList();
            return(serviceResponse);
        }
コード例 #2
0
        public async Task <ServiceResponse <List <GetCharacterDto> > > UpdateCharacter(GetCharacterDto characterToModify)
        {
            var response = new ServiceResponse <List <GetCharacterDto> >();

            try
            {
                Character characterSelected = await _context.Characters.FirstAsync(l => l.Id == characterToModify.Id);

                characterSelected.HitPoints    = characterToModify.HitPoints;
                characterSelected.Class        = characterToModify.Class;
                characterSelected.Defense      = characterToModify.Defense;
                characterSelected.Intelligence = characterToModify.Intelligence;
                characterSelected.Name         = characterToModify.Name;
                characterSelected.Strength     = characterToModify.Strength;

                _context.Characters.Update(characterSelected);
                await _context.SaveChangesAsync();
            }
            catch (Exception exception)
            {
                response.Success = false;
                response.Message = exception.Message;
            }


            response.Data = (_context.Characters.Select(l => _mapper.Map <GetCharacterDto>(l))).ToList();
            return(response);
        }
コード例 #3
0
 public async Task <IActionResult> Put(GetCharacterDto characterToModify)
 {
     return(Ok(await _characterService.UpdateCharacter(characterToModify)));
 }
コード例 #4
0
 public async Task <IActionResult> AddCharacter(GetCharacterDto newCharacter)
 {
     return(Ok(await _characterService.AddCharacter(newCharacter)));
 }