Exemple #1
0
        public async Task <IActionResult> GetUserVisit(int user, string visit)
        {
            IActionResult response = null;

            this._logger.LogInformation(LoggingEvents.GET_USER_VISIT, "Getting user visit for userId={user} and visitId={visit}", user, visit);
            var userVisit = await this.VisitsRepository.GetVisit(visit);

            //Validate
            if (userVisit != null && userVisit.User == user)
            {
                var tasks     = new List <Task>();
                var cityTask  = this.GeographyRepository.GetCityAsync(userVisit.CityId);
                var stateTask = this.GeographyRepository.GetStateAsync(userVisit.StateId);
                tasks.Add(cityTask);
                tasks.Add(stateTask);
                await Task.WhenAll(tasks.ToArray());

                var city  = cityTask.Result;
                var state = stateTask.Result;

                VisitRepresentation visitRepresentation = new VisitRepresentation()
                {
                    User    = userVisit.User,
                    Created = userVisit.Created,
                    City    = city.Name,
                    State   = state.Abbreviation,
                    VisitId = userVisit.VisitId,
                    Links   = new VisitRepresentationLinks()
                    {
                        Self = new Link()
                        {
                            Href = string.Format("/user/{0}/visit/{1}", userVisit.User, userVisit.VisitId)
                        }
                    }
                };
                response = this.Ok(visitRepresentation);
            }
            else
            {
                response = this.NotFound();
            }

            return(response);
        }
Exemple #2
0
        public async Task <IActionResult> PostUserVisit(int user, [FromBody] PostVisitRepresentation visit)
        {
            IActionResult response = null;

            this._logger.LogInformation(LoggingEvents.POST_USER_VISIT, "Post user visit for userId={user}", user);
            var claims = this.ExtractClaimsFromAuthorizationHeaderBearerToken(this.Request.Headers);

            //NOTE: Right now this always evaluates to true, because the BearerTokenDecryptor is hard coded to
            //return this claim.
            if (ClaimsChecker.IsAllowed("POST", "*", claims))
            {
                var claimsUserId = this.ExtractClaimsUserId(claims);
                if (claimsUserId.HasValue && user == claimsUserId.Value)
                {
                    //Validate input data
                    if (!string.IsNullOrWhiteSpace(visit.City) && visit.City.Length <= MAXCITYLENGTH &&
                        !string.IsNullOrWhiteSpace(visit.State) && visit.State.Length == MAXSTATELENGTH)
                    {
                        //TODO: Add rate limiting check here, so even if a valid user submits valid data,
                        //they can't create hundreds of these per minute, because it's expected to come
                        //from a real end user and not an automated system. If a backend system does call
                        //this API then the rate limiting can be made smarter or relaxed to a very large number.

                        var tasks     = new List <Task>();
                        var stateTask = this.GeographyRepository.GetStateByAbbreviationAsync(visit.State);
                        tasks.Add(stateTask);
                        var cityTask = this.GeographyRepository.GetCityAsync(visit.State, visit.City);
                        tasks.Add(cityTask);
                        var state = stateTask.Result;
                        var city  = cityTask.Result;

                        //Do all these tasks at once and wait for all to complete.
                        await Task.WhenAll(tasks);

                        //Persist to the repository
                        var userVisit = new Visit()
                        {
                            Created = DateTime.UtcNow,
                            User    = user,
                            CityId  = city.CityId,
                            StateId = state.StateId,
                            VisitId = Guid.NewGuid().ToString()
                        };
                        await this.VisitsRepository.SaveVisit(userVisit);


                        //Convert to representation.
                        var visitRepresentation = new VisitRepresentation()
                        {
                            City    = city.Name,
                            Created = userVisit.Created,
                            State   = state.Abbreviation,
                            User    = user,
                            VisitId = userVisit.VisitId,
                            Links   = new VisitRepresentationLinks()
                            {
                                Self = new Link()
                                {
                                    Href = string.Format("/user/{0}/visit/{1}", userVisit.User, userVisit.VisitId)
                                }
                            }
                        };

                        response = this.Ok(visitRepresentation);
                    }
                    else
                    {
                        response = this.BadRequest("City or state too long. Use city name and state abbreviation.");
                    }
                }
                else
                {
                    this._logger.LogWarning(LoggingEvents.POST_USER_VISIT, "Unauthorized attempt to post user visit for userId={user}", user);
                    response = this.Unauthorized();
                }
            }
            else
            {
                this._logger.LogWarning(LoggingEvents.POST_USER_VISIT, "Unauthorized attempt to post user visit for userId={user}", user);
                response = this.Unauthorized();
            }

            return(response);
        }