예제 #1
0
        // Returns a specific hero
        public IHttpActionResult Get(string id)
        {
            Trace.TraceInformation($"Retrieving hero {id}");
            
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            // Try to resolve hero
            Guid heroId = _cryptographyService.DecryptGuid(id);
            Hero hero = _heroRepository.GetHero(heroId);
            
            if (hero == null)
            {
                return NotFound();
            }

            HeroViewModel heroViewModel = new HeroViewModel(hero, _cryptographyService);
            return Ok(heroViewModel);
        }
예제 #2
0
        // Creates and returns a hero.  The hero is created with the initial state of Inactive
        public IHttpActionResult Post(HeroPropertiesModel model)
        {
            Trace.TraceInformation($"Creating hero {model}");

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

            HeroState inactive = _heroRepository.HeroStates[HeroState.Inactive];            
            Hero hero = new Hero(model.Name, model.Type, inactive, model.Price.GetValueOrDefault());

            Validate(hero); // Adds validation to ModelState

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

            _heroRepository.Save(hero);
            HeroViewModel heroViewModel = new HeroViewModel(hero, _cryptographyService);
            return Ok(heroViewModel);
        }
예제 #3
0
        // Updates hero
        public IHttpActionResult Put(string id, HeroPropertiesModel model)
        {
            Trace.TraceInformation($"Updating hero {id}: {model}");

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

            // Try to resolve hero
            Guid heroId = _cryptographyService.DecryptGuid(id);            
            Hero hero = _heroRepository.GetHero(heroId);

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

            hero.Name = model.Name;
            hero.Type = model.Type;
            hero.State = model.State;
            hero.Price = model.Price.GetValueOrDefault();

            Validate(hero); // Adds validation to ModelState

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

            _heroRepository.Save(hero);
            HeroViewModel heroViewModel = new HeroViewModel(hero, _cryptographyService);
            return Ok(heroViewModel);
        }