Skip to content

Commit

Permalink
feat: entity and repo for main models of auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
SShlykov committed Mar 15, 2024
1 parent 794700e commit afa59ae
Show file tree
Hide file tree
Showing 43 changed files with 672 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ create table if not exists users
email varchar(255),
password_hash varchar(255),

deleted_by integer,
deleted_by uuid references users(id) on delete cascade,
access_template_id integer,
update_after bigint
);
Expand Down
47 changes: 47 additions & 0 deletions auth/internal/domain/entity/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package entity

import "github.com/jackc/pgx/v5"

type Role struct {
ID string
Name string
}

func (r Role) TableName() string {
return "roles"
}

func (r Role) AllFields() []string {
return []string{"id", "name"}
}

func (r Role) InsertOrUpdateFields() []string {
return []string{"name"}
}

func (r Role) EntityToInsertValues(impl *Role) []interface{} {
return []interface{}{
impl.Name,
}
}

func (r Role) ReadItem(row pgx.Row) (Role, error) {
var role Role
err := row.Scan(&role.ID, &role.Name)
if err != nil {
return Role{}, err
}
return role, nil
}

func (r Role) ReadList(rows pgx.Rows) ([]Role, error) {
var roles []Role
for rows.Next() {
role, err := r.ReadItem(rows)
if err != nil {
return nil, err
}
roles = append(roles, role)
}
return roles, nil
}
69 changes: 69 additions & 0 deletions auth/internal/domain/entity/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package entity

import (
"database/sql"
"github.com/jackc/pgx/v5"
"time"
)

type User struct {
ID string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt sql.Null[time.Time]

LoggedAt sql.Null[time.Time]
ConfirmedAt sql.Null[time.Time]

Login string // index
Email string // index

PasswordHash string

DeletedBy sql.Null[string]
AccessTemplateID sql.Null[int]
UpdateAfter sql.Null[int64]
}

func (u User) TableName() string {
return "users"
}

func (u User) AllFields() []string {
return []string{"id", "created_at", "updated_at", "deleted_at", "logged_at", "confirmed_at", "login",
"email", "password_hash", "deleted_by", "access_template_id", "update_after"}
}

func (u User) InsertOrUpdateFields() []string {
return []string{"logged_at", "confirmed_at", "login", "email", "password_hash", "deleted_by",
"access_template_id", "update_after"}
}

func (u User) EntityToInsertValues(impl *User) []interface{} {
return []interface{}{
impl.LoggedAt, impl.ConfirmedAt, impl.Login, impl.Email, impl.PasswordHash, impl.DeletedBy,
impl.AccessTemplateID, impl.UpdateAfter,
}
}

func (u User) ReadItem(row pgx.Row) (User, error) {
var user User
err := row.Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt, &user.DeletedAt, &user.LoggedAt, &user.ConfirmedAt,
&user.Login, &user.Email, &user.PasswordHash, &user.DeletedBy, &user.AccessTemplateID, &user.UpdateAfter)
if err != nil {
return User{}, err
}
return user, nil
}

func (u User) ReadList(rows pgx.Rows) ([]User, error) {
var users []User
for rows.Next() {
user, err := u.ReadItem(rows)
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
57 changes: 57 additions & 0 deletions auth/internal/domain/entity/user_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package entity

import "github.com/jackc/pgx/v5"

type UserProfile struct {
ID string
UserID string
FirstName string
LastName string
SecondName string
Locale string
Gender int
Phone string
Notes string
}

func (u UserProfile) TableName() string {
return "user_profiles"
}

func (u UserProfile) AllFields() []string {
return []string{"id", "user_id", "first_name", "last_name", "second_name", "locale",
"gender", "phone", "notes"}
}

func (u UserProfile) InsertOrUpdateFields() []string {
return []string{"user_id", "first_name", "last_name", "second_name", "locale", "gender", "phone", "notes"}
}

func (u UserProfile) EntityToInsertValues(impl *UserProfile) []interface{} {
return []interface{}{
impl.UserID, impl.FirstName, impl.LastName, impl.SecondName, impl.Locale, impl.Gender,
impl.Phone, impl.Notes}
}

func (u UserProfile) ReadItem(row pgx.Row) (UserProfile, error) {
var userProfile UserProfile
err := row.Scan(&userProfile.ID, &userProfile.UserID, &userProfile.FirstName, &userProfile.LastName,
&userProfile.SecondName, &userProfile.Locale, &userProfile.Gender, &userProfile.Phone, &userProfile.Notes)

if err != nil {
return UserProfile{}, err
}
return userProfile, nil
}

func (u UserProfile) ReadList(rows pgx.Rows) ([]UserProfile, error) {
var userProfiles []UserProfile
for rows.Next() {
userProfile, err := u.ReadItem(rows)
if err != nil {
return nil, err
}
userProfiles = append(userProfiles, userProfile)
}
return userProfiles, nil
}
53 changes: 53 additions & 0 deletions auth/internal/domain/entity/user_role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package entity

import (
"github.com/jackc/pgx/v5"
"time"
)

type UserRole struct {
ID string
UserID string
RoleID string
CreatedAt time.Time
UpdatedAt time.Time
}

func (ur UserRole) TableName() string {
return "user_roles"
}

func (ur UserRole) AllFields() []string {
return []string{"id", "user_id", "role_id", "created_at", "updated_at"}
}

func (ur UserRole) InsertOrUpdateFields() []string {
return []string{"user_id", "role_id", "created_at", "updated_at"}
}

func (ur UserRole) EntityToInsertValues(impl *UserRole) []interface{} {
return []interface{}{
impl.UserID, impl.RoleID, impl.CreatedAt, impl.UpdatedAt,
}
}

func (ur UserRole) ReadItem(row pgx.Row) (UserRole, error) {
var userRole UserRole
err := row.Scan(&userRole.ID, &userRole.UserID, &userRole.RoleID, &userRole.CreatedAt, &userRole.UpdatedAt)
if err != nil {
return UserRole{}, err
}
return userRole, nil
}

func (ur UserRole) ReadList(rows pgx.Rows) ([]UserRole, error) {
var userRoles []UserRole
for rows.Next() {
userRole, err := ur.ReadItem(rows)
if err != nil {
return nil, err
}
userRoles = append(userRoles, userRole)
}
return userRoles, nil
}
54 changes: 54 additions & 0 deletions auth/internal/domain/entity/user_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package entity

import (
"github.com/jackc/pgx/v5"
"time"
)

type UserToken struct {
ID string
UserID string
Token string
Context string
SentTo string
InsertedAt time.Time
}

func (ut UserToken) TableName() string {
return "user_tokens"
}

func (ut UserToken) AllFields() []string {
return []string{"id", "user_id", "token", "context", "sent_to", "inserted_at"}
}

func (ut UserToken) InsertOrUpdateFields() []string {
return []string{"user_id", "token", "context", "sent_to", "inserted_at"}
}

func (ut UserToken) EntityToInsertValues(impl *UserToken) []interface{} {
return []interface{}{
impl.UserID, impl.Token, impl.Context, impl.SentTo, impl.InsertedAt,
}
}

func (ut UserToken) ReadItem(row pgx.Row) (UserToken, error) {
var userToken UserToken
err := row.Scan(&userToken.ID, &userToken.UserID, &userToken.Token, &userToken.Context, &userToken.SentTo, &userToken.InsertedAt)
if err != nil {
return UserToken{}, err
}
return userToken, nil
}

func (ut UserToken) ReadList(rows pgx.Rows) ([]UserToken, error) {
var userTokens []UserToken
for rows.Next() {
userToken, err := ut.ReadItem(rows)
if err != nil {
return nil, err
}
userTokens = append(userTokens, userToken)
}
return userTokens, nil
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package pgrepo

import (
"context"
"github.com/SShlykov/zeitment/bookback/internal/models/dbutils"
"github.com/SShlykov/zeitment/postgres"
"github.com/SShlykov/zeitment/postgres/dbutils"
"github.com/jackc/pgx/v5"
"strings"
)
Expand Down
24 changes: 24 additions & 0 deletions auth/internal/infrastructure/repository/pgrepo/role_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pgrepo

import (
"github.com/SShlykov/zeitment/auth/internal/domain/entity"
"github.com/SShlykov/zeitment/postgres"
)

type RolesRepo interface {
Repository[entity.Role]
}

type rolesRepo struct {
repository[entity.Role]
}

func NewRolesRepository(db postgres.Client) RolesRepo {
return &rolesRepo{
repository: repository[entity.Role]{
Name: "RolesRepository",
entity: entity.Role{},
db: db,
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pgrepo

import (
"github.com/SShlykov/zeitment/auth/internal/domain/entity"
"github.com/SShlykov/zeitment/postgres"
)

type UserProfileRepo interface {
Repository[entity.UserProfile]
}

type userProfilesRepo struct {
repository[entity.UserProfile]
}

func NewUserProfilesRepository(db postgres.Client) UserProfileRepo {
return &userProfilesRepo{
repository: repository[entity.UserProfile]{
Name: "UserProfileRepository",
entity: entity.UserProfile{},
db: db,
},
}
}
24 changes: 24 additions & 0 deletions auth/internal/infrastructure/repository/pgrepo/user_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pgrepo

import (
"github.com/SShlykov/zeitment/auth/internal/domain/entity"
"github.com/SShlykov/zeitment/postgres"
)

type UsersRepo interface {
Repository[entity.User]
}

type usersRepo struct {
repository[entity.User]
}

func NewUsersRepository(db postgres.Client) UsersRepo {
return &usersRepo{
repository: repository[entity.User]{
Name: "UsersRepository",
entity: entity.User{},
db: db,
},
}
}
Loading

0 comments on commit afa59ae

Please sign in to comment.