private void PostAction(HttpListenerContext httpListenerContext, string[] route)
        {
            string          json;
            UserBeerRanking ranking;

            try
            {
                json = ReadJsonContent(httpListenerContext);

                ranking = JsonConvert.DeserializeObject <UserBeerRanking>(json);
            }
            catch (Exception e)
            {
                ErrorResponse
                .Get(HttpStatusCode.BadRequest)
                .Handle(httpListenerContext, $"Malformed or bad data provided in Json Body. Details - {e.Message}");
                return;
            }

            try
            {
                switch (route[0])
                {
                case "add":
                    SendResponse(httpListenerContext, beerRankingRepo.Insert(ranking));
                    break;

                case "update":
                    SendResponse(httpListenerContext, beerRankingRepo.Update(ranking));
                    break;

                default:
                    ErrorResponse
                    .Get(HttpStatusCode.BadRequest)
                    .Handle(httpListenerContext,
                            $"Bad Request - No '/rankings/{string.Join("/", route)}/...' exists");
                    break;
                }
            }
            catch (Exception)
            {
                ErrorResponse.Get(HttpStatusCode.InternalServerError).Handle(httpListenerContext,
                                                                             $"Route - '{string.Join("/", route)}'," +
                                                                             $" Content - '{json}'");
            }
        }
Beispiel #2
0
        public override TransactionResult <UserInfo> Update(UserInfo appObj)
        {
            const string sql = "UPDATE UserInfo SET " +
                               "UserName = @UserName " +
                               "UserEmail = @UserEmail " +
                               "FirstName = @FirstName " +
                               "LastName = @LastName " +
                               "Location = @Location " +
                               "UntappdId = @UntappdId " +
                               "WHERE UserId = @UserId;";

            var retVal = new TransactionResult <UserInfo>();

            try
            {
                using (var connection = new MySqlConnection(Config.DatabaseConnectionString))
                {
                    connection.Open();
                    connection.Execute(sql,
                                       new
                    {
                        appObj.UserName,
                        appObj.UserEmail,
                        appObj.FirstName,
                        appObj.LastName,
                        appObj.Location,
                        appObj.UntappdId,
                        appObj.UserId
                    });
                }

                var userRankingCollection = new List <TransactionResult <UserBeerRanking> >();

                appObj.BeerRankings.ForEach(r => r.User_FK = appObj.UserId);

                foreach (var ranking in appObj.BeerRankings)
                {
                    userRankingCollection.Add(rankRepo.Update(ranking));
                }

                var socialAccountsCollection = new List <TransactionResult <SocialMediaAccount> >();

                foreach (var account in appObj.SocialAccounts)
                {
                    socialAccountsCollection.Add(socialAccRepo.Update(account, appObj.UserId));
                }

                appObj.BeerRankings   = userRankingCollection.Where(e => e.Success).Select(e => e.Data).ToList();
                appObj.SocialAccounts = socialAccountsCollection.Where(e => e.Success).Select(e => e.Data).ToList();

                retVal.Data    = appObj;
                retVal.Success = true;
            }
            catch (Exception e)
            {
                Log.Error($"Could not update '{JsonConvert.SerializeObject(appObj)}.'", e);

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

            return(retVal);
        }