Exemplo n.º 1
0
        public async Task <ActionResult> GetStar(string id)
        {
            var star = await service
                       .GetStarAsync(id)
                       .ConfigureAwait(false);

            if (star == null)
            {
                return(NotFound(Error.NotFound));
            }

            return(Ok(star));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> AddPlanet(Planet planet)
        {
            if (!Request.Cookies.TryGetValue("token", out var token))
            {
                return(Unauthorized(Error.NotAllowed));
            }

            TokenInfo info;

            try
            {
                info = await this.authenticator
                       .ExtractTokenInfoAsync(token)
                       .ConfigureAwait(false);
            }
            catch
            {
                Response.Cookies.Delete("token");
                return(Unauthorized(Error.NotAllowed));
            }

            if (planet.StarId == null)
            {
                return(BadRequest(Error.Create("incorrect star id")));
            }

            if (info.Owner != planet.StarId)
            {
                return(Unauthorized(Error.NotAllowed));
            }

            var star = await service
                       .GetStarAsync(planet.StarId)
                       .ConfigureAwait(false);

            if (star == null)
            {
                return(NotFound(Error.NotAllowed));
            }

            if (star.Planets.Count >= MaxPlanetsCount)
            {
                return(BadRequest(Error.Create("maximum planets count")));
            }

            await service
            .AddPlanetAsync(planet, star)
            .ConfigureAwait(false);

            var newInfo = TokenInfo.Create(
                star.Id,
                star.Planets.Select(Hasher.Hash));

            var newToken = await this.authenticator
                           .GenerateTokenAsync(newInfo)
                           .ConfigureAwait(false);

            Response.Cookies.Append("token", newToken);

            return(CreatedAtRoute(nameof(GetPlanet), new { id = planet.Id }, planet));
        }