16 changed files with 503 additions and 0 deletions
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
namespace Bit.Core.Enums |
||||
{ |
||||
public enum TransactionType : byte |
||||
{ |
||||
Charge = 0, |
||||
Credit = 1, |
||||
PromotionalCredit = 2, |
||||
ReferralCredit = 3 |
||||
} |
||||
} |
||||
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
using System; |
||||
using Bit.Core.Enums; |
||||
using Bit.Core.Utilities; |
||||
|
||||
namespace Bit.Core.Models.Table |
||||
{ |
||||
public class Transaction : ITableObject<Guid> |
||||
{ |
||||
public Guid Id { get; set; } |
||||
public Guid? UserId { get; set; } |
||||
public Guid? OrganizationId { get; set; } |
||||
public TransactionType Type { get; set; } |
||||
public decimal Amount { get; set; } |
||||
public bool? Refunded { get; set; } |
||||
public decimal? RefundedAmount { get; set; } |
||||
public string Details { get; set; } |
||||
public PaymentMethodType? PaymentMethodType { get; set; } |
||||
public GatewayType? Gateway { get; set; } |
||||
public string GatewayId { get; set; } |
||||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow; |
||||
|
||||
public void SetNewId() |
||||
{ |
||||
Id = CoreHelpers.GenerateComb(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
using System; |
||||
using Bit.Core.Models.Table; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Bit.Core.Repositories |
||||
{ |
||||
public interface ITransactionRepository : IRepository<Transaction, Guid> |
||||
{ |
||||
Task<ICollection<Transaction>> GetManyByUserIdAsync(Guid userId); |
||||
Task<ICollection<Transaction>> GetManyByOrganizationIdAsync(Guid organizationId); |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
using System; |
||||
using Bit.Core.Models.Table; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
using Dapper; |
||||
using System.Data; |
||||
using System.Data.SqlClient; |
||||
using System.Linq; |
||||
|
||||
namespace Bit.Core.Repositories.SqlServer |
||||
{ |
||||
public class TransactionRepository : Repository<Transaction, Guid>, ITransactionRepository |
||||
{ |
||||
public TransactionRepository(GlobalSettings globalSettings) |
||||
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString) |
||||
{ } |
||||
|
||||
public TransactionRepository(string connectionString, string readOnlyConnectionString) |
||||
: base(connectionString, readOnlyConnectionString) |
||||
{ } |
||||
|
||||
public async Task<ICollection<Transaction>> GetManyByUserIdAsync(Guid userId) |
||||
{ |
||||
using(var connection = new SqlConnection(ConnectionString)) |
||||
{ |
||||
var results = await connection.QueryAsync<Transaction>( |
||||
$"[{Schema}].[Transaction_ReadByUserId]", |
||||
new { UserId = userId }, |
||||
commandType: CommandType.StoredProcedure); |
||||
|
||||
return results.ToList(); |
||||
} |
||||
} |
||||
|
||||
public async Task<ICollection<Transaction>> GetManyByOrganizationIdAsync(Guid organizationId) |
||||
{ |
||||
using(var connection = new SqlConnection(ConnectionString)) |
||||
{ |
||||
var results = await connection.QueryAsync<Transaction>( |
||||
$"[{Schema}].[Transaction_ReadByOrganizationId]", |
||||
new { OrganizationId = organizationId }, |
||||
commandType: CommandType.StoredProcedure); |
||||
|
||||
return results.ToList(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_Create] |
||||
@Id UNIQUEIDENTIFIER, |
||||
@UserId UNIQUEIDENTIFIER, |
||||
@OrganizationId UNIQUEIDENTIFIER, |
||||
@Type TINYINT, |
||||
@Amount MONEY, |
||||
@Refunded BIT, |
||||
@RefundedAmount MONEY, |
||||
@Details NVARCHAR(100), |
||||
@PaymentMethodType TINYINT, |
||||
@Gateway TINYINT, |
||||
@GatewayId VARCHAR(50), |
||||
@CreationDate DATETIME2(7) |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
INSERT INTO [dbo].[Transaction] |
||||
( |
||||
[Id], |
||||
[UserId], |
||||
[OrganizationId], |
||||
[Type], |
||||
[Amount], |
||||
[Refunded], |
||||
[RefundedAmount], |
||||
[Details], |
||||
[PaymentMethodType], |
||||
[Gateway], |
||||
[GatewayId], |
||||
[CreationDate] |
||||
) |
||||
VALUES |
||||
( |
||||
@Id, |
||||
@UserId, |
||||
@OrganizationId, |
||||
@Type, |
||||
@Amount, |
||||
@Refunded, |
||||
@RefundedAmount, |
||||
@Details, |
||||
@PaymentMethodType, |
||||
@Gateway, |
||||
@GatewayId, |
||||
@CreationDate |
||||
) |
||||
END |
||||
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_DeleteById] |
||||
@Id UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
DELETE |
||||
FROM |
||||
[dbo].[Transaction] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadById] |
||||
@Id UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadByOrganizationId] |
||||
@OrganizationId UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[UserId] = NULL |
||||
AND [OrganizationId] = @OrganizationId |
||||
END |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadByUserId] |
||||
@UserId UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[UserId] = @UserId |
||||
END |
||||
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
CREATE PROCEDURE [dbo].[Transaction_Update] |
||||
@Id UNIQUEIDENTIFIER, |
||||
@UserId UNIQUEIDENTIFIER, |
||||
@OrganizationId UNIQUEIDENTIFIER, |
||||
@Type TINYINT, |
||||
@Amount MONEY, |
||||
@Refunded BIT, |
||||
@RefundedAmount MONEY, |
||||
@Details NVARCHAR(100), |
||||
@PaymentMethodType TINYINT, |
||||
@Gateway TINYINT, |
||||
@GatewayId VARCHAR(50), |
||||
@CreationDate DATETIME2(7) |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
UPDATE |
||||
[dbo].[Transaction] |
||||
SET |
||||
[UserId] = @UserId, |
||||
[OrganizationId] = @OrganizationId, |
||||
[Type] = @Type, |
||||
[Amount] = @Amount, |
||||
[Refunded] = @Refunded, |
||||
[RefundedAmount] = @RefundedAmount, |
||||
[Details] = @Details, |
||||
[PaymentMethodType] = @PaymentMethodType, |
||||
[Gateway] = @Gateway, |
||||
[GatewayId] = @GatewayId, |
||||
[CreationDate] = @CreationDate |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
CREATE TABLE [dbo].[Transaction] ( |
||||
[Id] UNIQUEIDENTIFIER NOT NULL, |
||||
[UserId] UNIQUEIDENTIFIER NULL, |
||||
[OrganizationId] UNIQUEIDENTIFIER NULL, |
||||
[Type] TINYINT NOT NULL, |
||||
[Amount] MONEY NOT NULL, |
||||
[Refunded] BIT NULL, |
||||
[RefundedAmount] MONEY NULL, |
||||
[Details] NVARCHAR(100) NULL, |
||||
[PaymentMethodType] TINYINT NULL, |
||||
[Gateway] TINYINT NULL, |
||||
[GatewayId] VARCHAR(50) NULL, |
||||
[CreationDate] DATETIME2 (7) NOT NULL, |
||||
CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED ([Id] ASC), |
||||
CONSTRAINT [FK_Transaction_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE, |
||||
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE |
||||
); |
||||
|
||||
|
||||
GO |
||||
CREATE NONCLUSTERED INDEX [IX_Transaction_Gateway_GatewayId] |
||||
ON [dbo].[Transaction]([Gateway] ASC, [GatewayId] ASC); |
||||
|
||||
|
||||
GO |
||||
CREATE NONCLUSTERED INDEX [IX_Transaction_UserId_OrganizationId_CreationDate] |
||||
ON [dbo].[Transaction]([UserId] ASC, [OrganizationId] ASC, [CreationDate] ASC); |
||||
|
||||
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
CREATE VIEW [dbo].[TransactionView] |
||||
AS |
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[Transaction] |
||||
@ -0,0 +1,224 @@
@@ -0,0 +1,224 @@
|
||||
IF OBJECT_ID('[dbo].[Transaction]') IS NULL |
||||
BEGIN |
||||
CREATE TABLE [dbo].[Transaction] ( |
||||
[Id] UNIQUEIDENTIFIER NOT NULL, |
||||
[UserId] UNIQUEIDENTIFIER NULL, |
||||
[OrganizationId] UNIQUEIDENTIFIER NULL, |
||||
[Type] TINYINT NOT NULL, |
||||
[Amount] MONEY NOT NULL, |
||||
[Refunded] BIT NULL, |
||||
[RefundedAmount] MONEY NULL, |
||||
[Details] NVARCHAR(100) NULL, |
||||
[PaymentMethodType] TINYINT NULL, |
||||
[Gateway] TINYINT NULL, |
||||
[GatewayId] VARCHAR(50) NULL, |
||||
[CreationDate] DATETIME2 (7) NOT NULL, |
||||
CONSTRAINT [PK_Transaction] PRIMARY KEY CLUSTERED ([Id] ASC), |
||||
CONSTRAINT [FK_Transaction_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id]) ON DELETE CASCADE, |
||||
CONSTRAINT [FK_Transaction_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]) ON DELETE CASCADE |
||||
); |
||||
|
||||
CREATE NONCLUSTERED INDEX [IX_Transaction_Gateway_GatewayId] |
||||
ON [dbo].[Transaction]([Gateway] ASC, [GatewayId] ASC); |
||||
|
||||
|
||||
CREATE NONCLUSTERED INDEX [IX_Transaction_UserId_OrganizationId_CreationDate] |
||||
ON [dbo].[Transaction]([UserId] ASC, [OrganizationId] ASC, [CreationDate] ASC); |
||||
END |
||||
GO |
||||
|
||||
IF EXISTS(SELECT * FROM sys.views WHERE [Name] = 'TransactionView') |
||||
BEGIN |
||||
DROP VIEW [dbo].[TransactionView] |
||||
END |
||||
GO |
||||
|
||||
CREATE VIEW [dbo].[TransactionView] |
||||
AS |
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[Transaction] |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_Create]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_Create] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_Create] |
||||
@Id UNIQUEIDENTIFIER, |
||||
@UserId UNIQUEIDENTIFIER, |
||||
@OrganizationId UNIQUEIDENTIFIER, |
||||
@Type TINYINT, |
||||
@Amount MONEY, |
||||
@Refunded BIT, |
||||
@RefundedAmount MONEY, |
||||
@Details NVARCHAR(100), |
||||
@PaymentMethodType TINYINT, |
||||
@Gateway TINYINT, |
||||
@GatewayId VARCHAR(50), |
||||
@CreationDate DATETIME2(7) |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
INSERT INTO [dbo].[Transaction] |
||||
( |
||||
[Id], |
||||
[UserId], |
||||
[OrganizationId], |
||||
[Type], |
||||
[Amount], |
||||
[Refunded], |
||||
[RefundedAmount], |
||||
[Details], |
||||
[PaymentMethodType], |
||||
[Gateway], |
||||
[GatewayId], |
||||
[CreationDate] |
||||
) |
||||
VALUES |
||||
( |
||||
@Id, |
||||
@UserId, |
||||
@OrganizationId, |
||||
@Type, |
||||
@Amount, |
||||
@Refunded, |
||||
@RefundedAmount, |
||||
@Details, |
||||
@PaymentMethodType, |
||||
@Gateway, |
||||
@GatewayId, |
||||
@CreationDate |
||||
) |
||||
END |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_DeleteById]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_DeleteById] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_DeleteById] |
||||
@Id UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
DELETE |
||||
FROM |
||||
[dbo].[Transaction] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_ReadById]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_ReadById] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadById] |
||||
@Id UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_ReadByOrganizationId]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_ReadByOrganizationId] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadByOrganizationId] |
||||
@OrganizationId UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[UserId] = NULL |
||||
AND [OrganizationId] = @OrganizationId |
||||
END |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_ReadByUserId]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_ReadByUserId] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_ReadByUserId] |
||||
@UserId UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[TransactionView] |
||||
WHERE |
||||
[UserId] = @UserId |
||||
END |
||||
GO |
||||
|
||||
IF OBJECT_ID('[dbo].[Transaction_Update]') IS NOT NULL |
||||
BEGIN |
||||
DROP PROCEDURE [dbo].[Transaction_Update] |
||||
END |
||||
GO |
||||
|
||||
CREATE PROCEDURE [dbo].[Transaction_Update] |
||||
@Id UNIQUEIDENTIFIER, |
||||
@UserId UNIQUEIDENTIFIER, |
||||
@OrganizationId UNIQUEIDENTIFIER, |
||||
@Type TINYINT, |
||||
@Amount MONEY, |
||||
@Refunded BIT, |
||||
@RefundedAmount MONEY, |
||||
@Details NVARCHAR(100), |
||||
@PaymentMethodType TINYINT, |
||||
@Gateway TINYINT, |
||||
@GatewayId VARCHAR(50), |
||||
@CreationDate DATETIME2(7) |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
UPDATE |
||||
[dbo].[Transaction] |
||||
SET |
||||
[UserId] = @UserId, |
||||
[OrganizationId] = @OrganizationId, |
||||
[Type] = @Type, |
||||
[Amount] = @Amount, |
||||
[Refunded] = @Refunded, |
||||
[RefundedAmount] = @RefundedAmount, |
||||
[Details] = @Details, |
||||
[PaymentMethodType] = @PaymentMethodType, |
||||
[Gateway] = @Gateway, |
||||
[GatewayId] = @GatewayId, |
||||
[CreationDate] = @CreationDate |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
GO |
||||
Loading…
Reference in new issue