public IActionResult Create([FromBody] CreatePlayerRequest createPlayerRequest)
        {
            var playerId = _playerService.Create(createPlayerRequest);

            if (playerId == null)
            {
                return(BadRequest());
            }

            return(Created($"player/{playerId}", new { id = playerId }));
        }
Example #2
0
        public int?Create(CreatePlayerRequest createPlayerRequest)
        {
            var teamDetails = _teamRepository.GetDetails(createPlayerRequest.TeamCode);

            if (teamDetails != null && teamDetails.ActivePlayers.Count() < 10)
            {
                return(_playerRepository.Create(createPlayerRequest));
            }

            return(null);
        }
Example #3
0
        public int?Create(CreatePlayerRequest createPlayerRequest)
        {
            using (var dbConnection = Db.CreateConnection())
            {
                var dbTransaction = dbConnection.BeginTransaction();

                using (var dbCommand = dbConnection.CreateCommand())
                {
                    try
                    {
                        dbCommand.Transaction = dbTransaction;

                        dbCommand.CommandText = @"
                                        INSERT INTO
                                            player(first_name, last_name)
                                            VALUES (@first_name, @last_name);
                                            SELECT SCOPE_IDENTITY()";

                        var firstNameParameter = dbCommand.CreateParameter();
                        firstNameParameter.DbType        = System.Data.DbType.String;
                        firstNameParameter.ParameterName = "@first_name";
                        firstNameParameter.Value         = createPlayerRequest.FirstName;
                        dbCommand.Parameters.Add(firstNameParameter);

                        var lastNameParameter = dbCommand.CreateParameter();
                        lastNameParameter.DbType        = System.Data.DbType.String;
                        lastNameParameter.ParameterName = "@last_name";
                        lastNameParameter.Value         = createPlayerRequest.LastName;
                        dbCommand.Parameters.Add(lastNameParameter);

                        var playerId = Convert.ToInt32(dbCommand.ExecuteScalar());

                        dbCommand.Parameters.Clear();

                        dbCommand.CommandText = @"
                                        INSERT INTO
                                            roster_transaction(roster_transaction_type_id, player_id, team_code, effective_date)
                                            VALUES (@roster_transaction_type_id, @player_id, @team_code, @effective_date)";

                        var rosterTransactionTypeParameter = dbCommand.CreateParameter();
                        rosterTransactionTypeParameter.DbType        = System.Data.DbType.Int32;
                        rosterTransactionTypeParameter.ParameterName = "@roster_transaction_type_id";
                        rosterTransactionTypeParameter.Value         = RosterTransactionType.Signed;
                        dbCommand.Parameters.Add(rosterTransactionTypeParameter);

                        var playerIdParameter = dbCommand.CreateParameter();
                        playerIdParameter.DbType        = System.Data.DbType.Int32;
                        playerIdParameter.ParameterName = "@player_id";
                        playerIdParameter.Value         = playerId;
                        dbCommand.Parameters.Add(playerIdParameter);

                        var teamCodeParameter = dbCommand.CreateParameter();
                        teamCodeParameter.DbType        = System.Data.DbType.String;
                        teamCodeParameter.ParameterName = "@team_code";
                        teamCodeParameter.Value         = createPlayerRequest.TeamCode;
                        dbCommand.Parameters.Add(teamCodeParameter);

                        var effectiveDateParameter = dbCommand.CreateParameter();
                        effectiveDateParameter.DbType        = System.Data.DbType.DateTime;
                        effectiveDateParameter.ParameterName = "@effective_date";
                        effectiveDateParameter.Value         = createPlayerRequest.EffectiveDate;
                        dbCommand.Parameters.Add(effectiveDateParameter);

                        dbCommand.ExecuteNonQuery();

                        dbTransaction.Commit();

                        return(playerId);
                    }
                    catch (Exception exception)
                    {
                        try
                        {
                            dbTransaction.Rollback();
                        }
                        catch (Exception exception2)
                        {
                            //TODO: something smart
                        }
                    }
                }
            }

            return(null);
        }