Ejemplo n.º 1
0
        public IActionResult Create()
        {
            var model = new CharacterInputModel()
            {
                Subraces   = _subraceData.GetAllPlayable(),
                Alignments = _alignmentData.GetAll(),
                Classes    = _classData.GetAll()
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] CharacterInputModel value)
        {
            if (value == null)
            {
                return(BadRequest("Invalid input"));
            }

            var result = await _charactersService.UpdateAsync(id, value);

            if (result == null)
            {
                return(BadRequest($"Update of character {id} failed"));
            }

            return(Ok(result));
        }
Ejemplo n.º 3
0
        public IActionResult Create(CharacterEditModel model)
        {
            if (ModelState.IsValid)
            {
                var subrace   = _subraceData.Get(model.Subrace);
                var alignment = _alignmentData.Get(model.Alignment);
                var _class    = _classData.Get(model.Class);


                var newCharacter = new Character()
                {
                    Name         = model.Name,
                    Alignment    = alignment,
                    Subrace      = subrace,
                    Level        = model.Level,
                    Experience   = model.Experience,
                    Speed        = subrace.Speed,
                    Class        = _class,
                    Strength     = subrace.Strength + subrace.Race.Strength,
                    Wisdom       = subrace.Wisdom + subrace.Race.Wisdom,
                    Intelligence = subrace.Intelligence + subrace.Race.Intelligence,
                    Dexterity    = subrace.Dexterity + subrace.Race.Dexterity,
                    Constitution = subrace.Constitution + subrace.Race.Constitution,
                    Charisma     = subrace.Charisma + subrace.Race.Charisma
                };

                newCharacter = _characterData.Add(newCharacter);

                return(RedirectToAction(nameof(Details), new { id = newCharacter.Id }));
            }
            else
            {
                var viewModel = new CharacterInputModel()
                {
                    Subraces   = _subraceData.GetAllPlayable(),
                    Alignments = _alignmentData.GetAll(),
                    Classes    = _classData.GetAll()
                };

                return(View(viewModel));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PostAsync([FromBody] CharacterInputModel value)
        {
            if (value == null)
            {
                return(BadRequest("Invalid input"));
            }

            var result = await _charactersService.AddAsync(value);

            if (result == null)
            {
                return(BadRequest("Character not inserted"));
            }

            return(CreatedAtRoute("CharacterGetAsync",
                                  new
            {
                id = result.Id
            },
                                  result));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> CharactersList(string characterName, string realm)
        {
            ViewBag.Realms   = new RealmList().RealmNames;
            ViewBag.Races    = new RaceList().RaceNames;
            ViewBag.Classes  = new ClassList().ClassNames;
            ViewBag.Factions = new FactionList().FactionNames;

            if (!string.IsNullOrEmpty(characterName) && (characterName.Length < 2 || characterName.Length > 12))
            {
                ModelState.AddModelError("", "Character name must be between 2 and 12 characters!");
                return(View(new List <CharacterListViewModel>()));
            }

            var characterExistInRealm = await
                                        _context.Characters.Where(x => x.Name == characterName && x.Realm == realm).ToListAsync();

            if (characterExistInRealm.Count > 0)
            {
                var characterList = _mapper.Map <List <CharacterListViewModel> >(characterExistInRealm);

                return(View(characterList));
            }

            if (realm == "Realm")
            {
                var characterExist = await
                                     _context.Characters.Where(x => x.Name == characterName).ToListAsync();

                if (characterExist.Count > 0)
                {
                    var characterList = _mapper.Map <List <CharacterListViewModel> >(characterExist);

                    return(View(characterList));
                }
            }

            var characterApi = await _wowClient.GetCharacterAsync(realm, characterName);

            if (characterApi.StatusCode == HttpStatusCode.NotFound)
            {
                return(View(new List <CharacterListViewModel>()));
            }

            using (HttpContent content = characterApi.Content)
            {
                string jsonResult = await content.ReadAsStringAsync();

                var inputModel = CharacterInputModel.FromJson(jsonResult);
                var character  = _mapper.Map <Character>(inputModel);
                _context.Characters.Add(character);
                await _context.SaveChangesAsync();
            }

            var characters = _context.Characters
                             .Where(x => x.Name == characterName)
                             .ToList();

            var result = _mapper.Map <List <CharacterListViewModel> >(characters);

            return(View(result));
        }