Пример #1
0
        public IActionResult Put([FromBody] Models.Activity activity)
        {
            Models.Activity result = _activityRepository.UpdateActivity(activity);

            if (result != null)
            {
                return(Accepted(result));
            }
            else
            {
                return(StatusCode(500));
            }
        }
Пример #2
0
        public IActionResult Post([FromBody] Models.Activity activity)
        {
            var result = _activityRepository.CreateActivity(activity);

            if (result > 0)
            {
                return(StatusCode(201, result));
            }
            else
            {
                return(StatusCode(500));
            }
        }
Пример #3
0
        public int CreateActivity(Models.Activity activity)
        {
            int newId;

            string sql = $@"INSERT INTO dbo.Activity (AccountId, ActivityTypeId, Amount) 
                            VALUES (@AccountId, @ActivityTypeId, @Amount)
                            SELECT CAST(SCOPE_IDENTITY() as int)";

            try
            {
                _logger.LogDebug($@"{_className}: Creating Activity for Account {activity.AccountId}");
                using (var connection = new SqlConnection(_connectionString))
                {
                    newId = connection.Query <int>(sql, new { AccountId = activity.AccountId, ActivityTypeId = activity.ActivityTypeId, Amount = activity.Amount }).Single();
                }

                return(newId);
            }
            catch (Exception e)
            {
                _logger.LogError($@"{_className}: Error creating Activity for Account {activity.AccountId} - {e.Message}");
                return(-1);
            }
        }
Пример #4
0
        public Models.Activity UpdateActivity(Models.Activity activity)
        {
            string sql = $@"UPDATE dbo.Activity
                            SET AccountId = @AccountId,
                            ActivityTypeId = @ActivityTypeId,
                            Amount = @Amount
                            WHERE Id = @Id";

            try
            {
                _logger.LogDebug($@"{_className}: Updating Activity {activity.Id} to AccountId {activity.AccountId}, ActivityTypeId {activity.ActivityTypeId}, Amount {activity.Amount}");
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Execute(sql, new { Id = activity.Id, AccountId = activity.AccountId, ActivityTypeId = activity.ActivityTypeId, Amount = activity.Amount });
                }

                return(GetActivityById(activity.Id));
            }
            catch (Exception e)
            {
                _logger.LogError($@"{_className}: Error updating Activity {activity.Id} - {e.Message}");
                return(null);
            }
        }