init
This commit is contained in:
109
internal/adapter/storages/postgres/repository/user.go
Normal file
109
internal/adapter/storages/postgres/repository/user.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/aykhans/oh-my-chat/internal/adapter/storages/postgres/models"
|
||||
"github.com/aykhans/oh-my-chat/internal/core/domain"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db}
|
||||
}
|
||||
|
||||
func (userRepository *UserRepository) CreateUser(
|
||||
ctx context.Context,
|
||||
user *domain.User,
|
||||
) (*domain.User, error) {
|
||||
userModel := &models.User{
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
Password: user.Password,
|
||||
}
|
||||
tx := userRepository.db.Create(userModel)
|
||||
if tx.Error != nil {
|
||||
return nil, tx.Error
|
||||
}
|
||||
|
||||
user.ID = userModel.ID
|
||||
user.Username = userModel.Username
|
||||
user.Email = userModel.Email
|
||||
user.Password = userModel.Password
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (userRepository *UserRepository) IsUsernameExists(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
) (bool, error) {
|
||||
var count int64
|
||||
tx := userRepository.db.
|
||||
Table(models.UserTableName).
|
||||
Where("username = ?", username).
|
||||
Count(&count)
|
||||
if tx.Error != nil {
|
||||
return false, tx.Error
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (userRepository *UserRepository) IsEmailExists(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
) (bool, error) {
|
||||
var count int64
|
||||
tx := userRepository.db.
|
||||
Table(models.UserTableName).
|
||||
Where("email = ?", email).
|
||||
Count(&count)
|
||||
if tx.Error != nil {
|
||||
return false, tx.Error
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (userRepository *UserRepository) GetUserByEmail(
|
||||
ctx context.Context,
|
||||
email string,
|
||||
) (*domain.User, error) {
|
||||
user := &domain.User{}
|
||||
tx := userRepository.db.
|
||||
Table(models.UserTableName).
|
||||
Where("email = ?", email).
|
||||
First(user)
|
||||
if tx.Error != nil {
|
||||
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, domain.ErrDataNotFound
|
||||
}
|
||||
return nil, tx.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (userRepository *UserRepository) GetUserByUsername(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
) (*domain.User, error) {
|
||||
user := &domain.User{}
|
||||
tx := userRepository.db.
|
||||
Table(models.UserTableName).
|
||||
Where("username = ?", username).
|
||||
First(user)
|
||||
if tx.Error != nil {
|
||||
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, domain.ErrDataNotFound
|
||||
}
|
||||
return nil, tx.Error
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
Reference in New Issue
Block a user