database

package
v0.0.0-...-953ae9e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package database witholds database specific utilities for the user microservice This package mainly deals with interactions with the postgresql database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAndSaveTestSubscription

func CreateAndSaveTestSubscription(t *testing.T, db *Database) (uint32, string, uint32)

CreateAndSaveTestSubscription generate a test subscription and saves it to a database

func CreateAndSaveTestUser

func CreateAndSaveTestUser(t *testing.T, db *Database) *model.UserORM

CreateAndSaveTestUser creates a test user and saves it to the database

func CreateAndSaveTestUserProfile

func CreateAndSaveTestUserProfile(t *testing.T, db *Database) uint32

CreateAndSaveTestUserProfile generate a test user profile and saves it to a database

func CreateTableForTests

func CreateTableForTests(db *gorm.DB) error

CreateTableForTests creates a given set of models based on a schema model for tests purposes

func CreateTablesOrMigrateSchemas

func CreateTablesOrMigrateSchemas(db *gorm.DB, logger *zap.Logger) error

CreateTablesOrMigrateSchemas creates a given set of models based on a schema if it does not exist or migrates the model schemas to the latest version

func DeleteCreatedEntities

func DeleteCreatedEntities(db *gorm.DB) func()

DeleteCreatedEntities sets up GORM `onCreate` hook and return a function that can be deferred to remove all the entities created after the hook was set up You can use it as

func TestSomething(t *testing.T){
    db, _ := gorm.Open(...)

    cleaner := DeleteCreatedEntities(db)
    defer cleaner()

}

func GenerateTestSubscription

func GenerateTestSubscription() (*model.Subscriptions, error)

GeneratetestSubscription creates a random user subscription for testing

func GenerateTestSubscriptionOrm

func GenerateTestSubscriptionOrm() (*model.SubscriptionsORM, error)

CreateAndSaveSubscriptionOrm create a random subscription and saves it to a database

func GenerateTestUser

func GenerateTestUser() (*model.User, error)

GenerateTestUser creates a test user

func GenerateTestUserOrm

func GenerateTestUserOrm() (*model.UserORM, error)

GenerateTestUser returns a new random user orm object

func GenerateTestUserProfile

func GenerateTestUserProfile() (*model.Profile, error)

GenerateTestUserProfile creates a test user profile

func GenerateTestUserProfileOrm

func GenerateTestUserProfileOrm() (*model.ProfileORM, error)

GenerateTestUserProfileOrm generates a user profile of orm type

func GenerateTokenAndUpdate

func GenerateTokenAndUpdate(n int, timeToUpdate *time.Time)

GenerateTokenAndUpdate updates the token expiration time

Types

type ComplexTransaction

type ComplexTransaction func(tx *gorm.DB) (interface{}, error)

ComplexTransaction is a type serving as a function decorator for complex database transactions

type Database

type Database struct {
	Engine *gorm.DB
	Logger *zap.Logger
}

Database witholds the a reference to the underlying database connection handle and a logging entity useful for interacting with the current database connection

func Initialize

func Initialize(connSettings string) *Database

Initialize creates a singular connection to the backend database instance

func New

func New(conn string, logger *zap.Logger) (*Database, error)

New creates a database connection and returns the connection object

func Setup

func Setup() (*Database, func())

Setup sets up database connection prior to testing

func (*Database) AddAdvisorToTeamOrUpdate

func (db *Database) AddAdvisorToTeamOrUpdate(ctx context.Context, teamID uint32, advisor model.User, isAddOperation bool) (model.Team, error)

AddAdvisorToTeamOrUpdate adds or updates an advisor to the team if the advisor doesnt currently exist or updates an existing one. This operation is performed within a transaction hence, any error occurring throughout the transaction lifecycle is returned, as well as the updated team account record

func (*Database) AddMemberToTeamOrUpdate

func (db *Database) AddMemberToTeamOrUpdate(ctx context.Context, teamID uint32, member model.User, isAddOperation bool) (model.Team, error)

AddMemberToTeamOrUpdate adds or updates a new member to a team if a team account exists. This operation is performed within a transaction hence any errors occurring throughout the transaction lifecycle are returned as well as the updated team account record

func (*Database) CheckIfUserExistsByUsername

func (db *Database) CheckIfUserExistsByUsername(username string) (bool, *model.UserORM, error)

CheckIfUserExistsByUsername checks that a given user exists based on a user account username

func (*Database) CreateOrUpdateTeamSubscription

func (db *Database) CreateOrUpdateTeamSubscription(ctx context.Context, teamID uint32, subscription model.Subscriptions) (*model.Team, error)

CreateOrUpdateTeamSubscription creates or updates a team subscription and ties it to a team account. A subscription is only successfully created if the team account of interest already exists. Additionally if a subscription of the same name exists already and is tied to the user id of interest, the subscription data is only updated if the subscription record stored in the database is 'inactive'.

func (*Database) CreateTeam

func (db *Database) CreateTeam(ctx context.Context, team model.Team, adminID uint32) (uint32, error)

CreateTeam creates a team record in the backend database if it doesn't already exists through a database transaction.

func (*Database) CreateTeamProfile

func (db *Database) CreateTeamProfile(ctx context.Context, teamID uint32, profile model.TeamProfile) (uint32, error)

CreateTeamProfile creates a team profile in the backend database assuming that a team account already exists.

func (*Database) CreateUser

func (db *Database) CreateUser(ctx context.Context, user model.User) (*model.User, error)

CreateUser creates a user account record in the database

func (*Database) CreateUserProfile

func (db *Database) CreateUserProfile(ctx context.Context, userID uint32, profile model.Profile) (*model.Profile, error)

CreateUserProfile creates a user profile and ties it to a user account record if the account record exists.

func (*Database) CreateUserSubscription

func (db *Database) CreateUserSubscription(ctx context.Context, userID uint32, subscription model.Subscriptions) error

CreateUserSubscription creates a subscription and ties it to a user account record if the account record exists, and the subscription does not. if the subscription does indeed exist and is inactive, it is reactivated.

func (*Database) DeleteTeamByID

func (db *Database) DeleteTeamByID(ctx context.Context, teamID uint32) error

DeleteTeamByID deletes a team based on a passed in id. It performs the operation in a transaction.

func (*Database) DeleteTeamProfileByID

func (db *Database) DeleteTeamProfileByID(_ context.Context, teamID uint32) error

DeleteTeamProfileByID deletes a team profile based on a passed in account id. It performs the operation in a transaction. Returned are any errors occuring during the transaction

func (*Database) DeleteUser

func (db *Database) DeleteUser(ctx context.Context, userID uint32) error

DeleteUser deletes a user account and any reference objects tied to the user

func (*Database) DeleteUserProfile

func (db *Database) DeleteUserProfile(ctx context.Context, userID, profileID uint32) error

DeleteUserProfile deletes a user profile tied to a user account

func (*Database) DeleteUserSubscription

func (db *Database) DeleteUserSubscription(ctx context.Context, userID, subscriptionID uint32) error

DeleteUserSubscription deletes a subscription tied to a user account

func (*Database) GetAllUserProfiles

func (db *Database) GetAllUserProfiles(ctx context.Context, limit uint32) ([]model.Profile, error)

GetAllUserProfiles queries the database and returns a set of user profile records. The upper bound on the number of records returned is defined by the limit input parameter.

func (*Database) GetAllUserProfilesByNationality

func (db *Database) GetAllUserProfilesByNationality(ctx context.Context, nationality string, limit uint32) ([]model.Profile, error)

GetAllUserProfilesByNationality queries the database for a list of profiles of a certain nationality. The maximal amount of records to return is defined by the limit input parameter

func (*Database) GetAllUserProfilesByType

func (db *Database) GetAllUserProfilesByType(ctx context.Context, profileType string, limit uint32) ([]model.Profile, error)

GetAllUserProfilesByType queries the database and returns all user profile records with a specified profile type. The upper bound on the number of records to return is defined by the limit input parameter.

func (*Database) GetAllUserSubscriptions

func (db *Database) GetAllUserSubscriptions(ctx context.Context, user_id uint32) ([]model.Subscriptions, error)

GetAllUserSubscriptions queries the database for a user based off of the user id and returns a set of subscriptions the user has

func (*Database) GetAllUsers

func (db *Database) GetAllUsers(ctx context.Context, limit uint32) ([]model.User, error)

GetAllUsers obtains user records. The max number of records returned is defined by the limit input parameter.

func (*Database) GetAllUsersByAccountType

func (db *Database) GetAllUsersByAccountType(ctx context.Context, accountType string, limit uint32) ([]model.User, error)

GetAllUsersByAccountType obtains user records by account type. The upper bound on the number of user records returned is defined by the limit input parameter.

func (*Database) GetAllUsersByIntent

func (db *Database) GetAllUsersByIntent(ctx context.Context, intent string, limit uint32) ([]model.User, error)

GetAllUsersByIntent queries the database for all user records based on intent. The upper bound on the number of user records returned is defined by the limit input parameter.

func (*Database) GetSubscriptionExistById

func (db *Database) GetSubscriptionExistById(userID, subscriptionID uint32) (bool, *model.SubscriptionsORM, error)

GetSubscriptionExistById checks that a given subscription exists by subscription id and the account id it is tied to

func (*Database) GetSubscriptionIfExistsByName

func (db *Database) GetSubscriptionIfExistsByName(subscriptionName string, userID uint32) (bool, *model.SubscriptionsORM, error)

GetSubscriptionIfExistsByName checks that a given subscription exists by name and user id to which it is tied to

func (*Database) GetTeamByID

func (db *Database) GetTeamByID(ctx context.Context, teamID uint32) (*model.Team, error)

GetTeamByID gets a team based on a passed in id. It performs the operation in a transaction.

func (*Database) GetTeamProfileByID

func (db *Database) GetTeamProfileByID(ctx context.Context, teamID uint32) (*model.TeamProfile, error)

GetTeamProfileByID gets a team profile based on a passed in team account id.

func (*Database) GetUserByID

func (db *Database) GetUserByID(ctx context.Context, userID uint32) (*model.User, error)

GetUserByID queries the database and obtains a user record by id

func (*Database) GetUserIfExists

func (db *Database) GetUserIfExists(userID uint32, username, email string) (bool, *model.UserORM, error)

GetUserIfExists checks that a given user exists based on user id

func (*Database) GetUserProfile

func (db *Database) GetUserProfile(ctx context.Context, userID uint32) (model.Profile, error)

GetUserProfile queries the database for a user profile record based on user account id

func (*Database) GetUserProfileIfExists

func (db *Database) GetUserProfileIfExists(userId uint32) (bool, *model.ProfileORM, error)

GetUserProfileIfExists checks that a given profile exists for a given user based on user id

func (*Database) PerformComplexTransaction

func (db *Database) PerformComplexTransaction(transaction ComplexTransaction) (interface{}, error)

PerformComplexTransaction takes as input an anonymous function witholding logic to perform within a transaction returning an abstract type. This function is then invoked within a transaction and depending on the occurrence of any specific errors, the transaction is either committed to the database or completely rolled back. This returns the result obtained from the invocation of the anonymous function as well as any error occuring throughout the transaction lifecycle.

func (*Database) PerformTransaction

func (db *Database) PerformTransaction(transaction Transaction) error

PerformTransaction takes as input an anonymous function witholding logic to perform within a transaction. This function is then invoked within a transaction. if unsuccessful or any error is raised throughout the transaction, then, the transaction is rolled back. Returned is any error occuring throughout the transaction lifecycle

func (*Database) UpdateTeam

func (db *Database) UpdateTeam(ctx context.Context, teamID uint32, team model.Team) (*model.Team, error)

UpdateTeam updates the team account if it already exists. This operation is performed within a transaction hence any errors occuring throughout the transaction lifecycle are returned as well as the updated team account record

func (*Database) UpdateTeamAdmin

func (db *Database) UpdateTeamAdmin(ctx context.Context, teamID uint32, adminID uint32, admin model.User) (*model.Team, error)

UpdateTeamAdmin updates a team admin if both the admin and team exist

func (*Database) UpdateTeamProfile

func (db *Database) UpdateTeamProfile(ctx context.Context, teamID uint32, profile model.TeamProfile) (*model.Team, error)

UpdateTeamProfile updates the team accounts profile if the team indeed exists

func (*Database) UpdateUser

func (db *Database) UpdateUser(ctx context.Context, userID uint32, user model.User) (*model.User, error)

UpdateUser updates a user record if it already exists in the backend

func (*Database) UpdateUserProfile

func (db *Database) UpdateUserProfile(ctx context.Context, userID, profileID uint32, newProfile model.Profile) (*model.Profile, error)

UpdateUserProfile updates a user profile tied to a user account if such profile exists

func (*Database) UpdateUserSubscription

func (db *Database) UpdateUserSubscription(ctx context.Context, userID uint32, subscriptionID uint32, newSubscription model.Subscriptions) (*model.Subscriptions, error)

UpdateUserSubscription updates a subscription tied to a user account if it exists

type IDatabase

type IDatabase interface {
	CreateUser(ctx context.Context, user model.User) error
	CreateUserProfile(userID int32, profile model.Profile) error
	CreateUserSubscription(userID int32, subscription model.Subscriptions) error

	GetUserByID(userID int32) (error, *model.User)
	DoesUserExists(userID int32, username, email string) (error, bool)
	DoesUserProfileExists(userID, profileID int32) (error, bool)
	DoesSubscriptionExist(subscriptionName string, userID int32) (error, bool)
	DoesSubscriptionExistById(userID, subscriptionID int32) (error, bool)

	UpdateUser(userID int32, user model.User) (error, *model.User)
	UpdateUserProfile(userID, profileID int32, profile model.Profile) (error, *model.Profile)
	UpdateUserSubscription(userID int32, subscriptionID int32, subscription model.Subscriptions) (error, *model.Subscriptions)

	DeleteUser(userID int32) error
	DeleteUserProfile(userID, profileID int32) error
	DeleteUserSubscription(userID, subscriptionID int32) error

	GetAllUsers(limit int32) (error, []model.User)
	GetAllUsersByAccountType(accountType string, limit int32) (error, []model.User)
	GetAllUsersByIntent(intent string, limit int32) (error, []model.User)
	GetUserProfile(userID int32) (error, model.Profile)
	GetUserProfiles(limit int32) (error, []model.Profile)
	GetAllUserProfilesByType(profileType string, limit int32) (error, []model.Profile)
	GetAllUserProfilesByNationality(nationality string, limit int32) (error, []model.Profile)
	GetAllUserSubscriptions(userID int32) (error, []model.Subscriptions)

	CreateTeam(team model.Team) error
	CreateTeamProfile(teamID int32, profile model.TeamProfile) error
	CreateOrUpdateTeamSubscription(teamID int32, subscription model.Subscriptions) (e0 error, m1 *model.Team)

	AddMemberToTeamOrUpdate(teamID int32, member model.User, isAddOperation bool) (e0 error, m1 model.Team)
	AddAdvisorToTeamOrUpdate(teamID int32, advisor model.User, isAddOperation bool) (e0 error, m1 model.Team)
	UpdateTeam(teamID int32, team model.Team) (e0 error, m1 *model.Team)
	UpdateTeamProfile(teamID, teamProfileID int32, profile model.TeamProfile) (error, *model.Team)
	UpdateTeamAdmin(teamID, adminID int32, admin model.User) (error, *model.Team)
}

IDatabase defines the interface definition for this service's database

type Transaction

type Transaction func(tx *gorm.DB) error

Transaction is a type serving as a function decorator for common database transactions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL