示例#1
0
        public TransactionResult <UserInfo> FindById(int id)
        {
            const string sql = "SELECT * FROM UserInfo WHERE " +
                               "UserId = @Id";

            var retVal = new TransactionResult <UserInfo>();

            try
            {
                using (var connection = new MySqlConnection(Config.DatabaseConnectionString))
                {
                    connection.Open();
                    retVal.Data = connection.Query <UserInfo>(sql,
                                                              new
                    {
                        id
                    }).FirstOrDefault();
                }

                if (retVal.Data != null)
                {
                    var accounts = socialAccRepo.FindByUserId(retVal.Data.UserId).Data;

                    retVal.Data.SocialAccounts = accounts?.ToList();

                    var rankings = rankRepo.FindAllByUserId(retVal.Data.UserId).Data;

                    retVal.Data.BeerRankings = rankings?.ToList();
                }

                retVal.Success = true;
            }
            catch (Exception e)
            {
                Log.Error($"Could not perform FIND using UserId - '{id}'", e);

                retVal.Success = false;
                retVal.Details = e.Message;
            }

            return(retVal);
        }
        private void GetAction(HttpListenerContext httpListenerContext, string[] route)
        {
            try
            {
                switch (route[0])
                {
                case "userId":
                    var uId = int.Parse(route[1]);
                    if (route.Length < 3)
                    {
                        SendResponse(httpListenerContext, beerRankingRepo.FindAllByUserId(uId));
                    }
                    else if (string.Equals(route[2], "beerId") && int.TryParse(route[3], out var oId))
                    {
                        SendResponse(httpListenerContext, beerRankingRepo.FindSingleByUserAndBeerId(uId, oId));
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                case "beerId":
                    var bId = int.Parse(route[1]);
                    if (route.Length < 3)
                    {
                        SendResponse(httpListenerContext, beerRankingRepo.FindAllByBeerId(bId));
                    }
                    else if (string.Equals(route[2], "userId") && int.TryParse(route[3], out var oId))
                    {
                        SendResponse(httpListenerContext, beerRankingRepo.FindSingleByUserAndBeerId(oId, bId));
                    }
                    else
                    {
                        goto default;
                    }
                    break;

                case "untappdId":
                    var untappdId = int.Parse(route[1]);
                    SendResponse(httpListenerContext, beerRankingRepo.FindAllByUntappdId(untappdId));
                    break;

                case "breweryDbId":
                    var breweryDbId = int.Parse(route[1]);
                    SendResponse(httpListenerContext, beerRankingRepo.FindAllByBreweryDbId(breweryDbId));
                    break;

                case "help":
                    SendOKResponseAndPayload(httpListenerContext,
                                             JsonConvert.SerializeObject(EndpointDocumentation, Formatting.Indented));
                    break;

                default:
                    ErrorResponse
                    .Get(HttpStatusCode.BadRequest)
                    .Handle(httpListenerContext,
                            $"Bad Request - No '/rankings/{string.Join("/", route)}/...' exists");
                    break;
                }
            }
            catch (Exception e)
            {
                if (e is ArgumentNullException || e is ArgumentException || e is FormatException ||
                    e is OverflowException)
                {
                    ErrorResponse.Get(HttpStatusCode.BadRequest).Handle(httpListenerContext, "Badly formed Id");
                }
                else
                {
                    ErrorResponse.Get(HttpStatusCode.InternalServerError).Handle(httpListenerContext, e.Message);
                }
            }
        }