I am batman
This commit is contained in:
28
services/user-service/internal/database/database.go
Normal file
28
services/user-service/internal/database/database.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"go-microservices/services/user-service/internal/models"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Init() (*gorm.DB, error) {
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
if dsn == "" {
|
||||
dsn = "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=UTC"
|
||||
}
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(&models.User{}, &models.Client{}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
27
services/user-service/internal/models/models.go
Normal file
27
services/user-service/internal/models/models.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Email string `gorm:"uniqueIndex;not null"`
|
||||
Active bool `gorm:"default:true"`
|
||||
Address string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Email string `gorm:"uniqueIndex;not null"`
|
||||
Name string
|
||||
Role string `gorm:"default:User"`
|
||||
Active bool `gorm:"default:true"`
|
||||
Username string `gorm:"uniqueIndex"`
|
||||
Address string
|
||||
PhoneNumber string
|
||||
ProfilePhoto []byte `gorm:"type:bytea"`
|
||||
ClientID *uint
|
||||
Client *Client
|
||||
}
|
||||
7
services/user-service/internal/repository/repository.go
Normal file
7
services/user-service/internal/repository/repository.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package repository
|
||||
|
||||
// TODO: Add database repository implementations
|
||||
// Example repositories:
|
||||
// - UserRepository for user data access
|
||||
// - ProfileRepository for profile management
|
||||
// - PreferenceRepository for user settings
|
||||
13
services/user-service/internal/repository/repository_impl.go
Normal file
13
services/user-service/internal/repository/repository_impl.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{DB: db}
|
||||
}
|
||||
49
services/user-service/internal/server/server.go
Normal file
49
services/user-service/internal/server/server.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "go-microservices/proto/user"
|
||||
|
||||
"go-microservices/services/user-service/internal/repository"
|
||||
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type UserServer struct {
|
||||
pb.UnimplementedUserServiceServer
|
||||
repo *repository.Repository
|
||||
}
|
||||
|
||||
func NewUserServer(repo *repository.Repository) *UserServer {
|
||||
return &UserServer{repo: repo}
|
||||
}
|
||||
|
||||
func (s *UserServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
// TODO: Implement create user logic
|
||||
user := &pb.User{Id: "1", Username: req.Username, Email: req.Email, Bio: req.Bio, AvatarUrl: req.AvatarUrl, CreatedAt: 0, UpdatedAt: 0}
|
||||
return &pb.CreateUserResponse{User: user}, nil
|
||||
}
|
||||
|
||||
func (s *UserServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
// TODO: Implement get user logic
|
||||
user := &pb.User{Id: req.Id, Username: "user", Email: "user@example.com", Bio: "bio", AvatarUrl: "", CreatedAt: 0, UpdatedAt: 0}
|
||||
return &pb.GetUserResponse{User: user}, nil
|
||||
}
|
||||
|
||||
func (s *UserServer) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserResponse, error) {
|
||||
// TODO: Implement update user logic
|
||||
user := &pb.User{Id: req.Id, Username: req.Username, Email: req.Email, Bio: req.Bio, AvatarUrl: req.AvatarUrl, CreatedAt: 0, UpdatedAt: 0}
|
||||
return &pb.UpdateUserResponse{User: user}, nil
|
||||
}
|
||||
|
||||
func (s *UserServer) DeleteUser(ctx context.Context, req *pb.DeleteUserRequest) (*emptypb.Empty, error) {
|
||||
// TODO: Implement delete user logic
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (s *UserServer) ListUsers(ctx context.Context, req *pb.ListUsersRequest) (*pb.ListUsersResponse, error) {
|
||||
// TODO: Implement list users logic
|
||||
users := []*pb.User{}
|
||||
return &pb.ListUsersResponse{Users: users}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user