fix issues
This commit is contained in:
@@ -5,11 +5,13 @@ import (
|
||||
|
||||
"datarush/internal/competition/domain"
|
||||
pb "datarush/pkg/api/competition"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ICompetitionService defines the interface for business logic.
|
||||
@@ -72,7 +74,7 @@ func (h *CompetitionHandler) EditCompetition(ctx context.Context, req *pb.Compet
|
||||
return fromDomainCompetition(updatedComp), nil
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*pb.DeleteCompetitionResponse, error) {
|
||||
func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*emptypb.Empty, error) {
|
||||
id, err := uuid.Parse(req.GetCompetitionId())
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "invalid competition id format")
|
||||
@@ -80,7 +82,7 @@ func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.Dele
|
||||
if err := h.service.DeleteCompetition(ctx, id); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to delete competition: %v", err)
|
||||
}
|
||||
return &pb.DeleteCompetitionResponse{Success: true}, nil
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (h *CompetitionHandler) ListCompetitions(ctx context.Context, req *pb.ListCompetitionsRequest) (*pb.ListCompetitionsResponse, error) {
|
||||
@@ -196,4 +198,4 @@ func toDomainCompetitionType(ct pb.CompetitionType) domain.CompetitionType {
|
||||
|
||||
func fromDomainCompetitionType(ct domain.CompetitionType) pb.CompetitionType {
|
||||
return pb.CompetitionType(pb.CompetitionType_value["COMPETITION_TYPE_"+string(ct)])
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,12 @@ const (
|
||||
competitionCachePrefix = "competition:"
|
||||
)
|
||||
|
||||
|
||||
type CompetitionRepository struct {
|
||||
db *sql.DB
|
||||
db *sql.DB
|
||||
redisClient *redis.Client
|
||||
cacheEnabled bool
|
||||
}
|
||||
|
||||
|
||||
func NewCompetitionRepository(db *sql.DB, redisClient *redis.Client, cacheEnabled bool) repository.CompetitionRepository {
|
||||
return &CompetitionRepository{
|
||||
db: db,
|
||||
@@ -149,7 +147,7 @@ func (r *CompetitionRepository) Delete(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
return nil
|
||||
}
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]*domain.Competition, int, error) {
|
||||
func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]domain.Competition, int, error) {
|
||||
var args []interface{}
|
||||
var whereClauses []string
|
||||
argId := 1
|
||||
@@ -200,7 +198,7 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var competitions []*domain.Competition
|
||||
var competitions []domain.Competition
|
||||
for rows.Next() {
|
||||
var c domain.Competition
|
||||
err := rows.Scan(
|
||||
@@ -219,7 +217,7 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
competitions = append(competitions, &c)
|
||||
competitions = append(competitions, c)
|
||||
}
|
||||
|
||||
return competitions, total, nil
|
||||
@@ -237,4 +235,4 @@ func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, s
|
||||
}
|
||||
|
||||
return r.Get(ctx, id)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
@@ -14,11 +13,9 @@ import (
|
||||
"datarush/internal/competition/service"
|
||||
pb "datarush/pkg/api/competition"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
@@ -54,12 +51,6 @@ func (s *Server) Start() error {
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
if err := s.startHTTPServer(); err != nil {
|
||||
log.Fatalf("failed to start HTTP server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Println("competition service started")
|
||||
return nil
|
||||
}
|
||||
@@ -89,50 +80,13 @@ func (s *Server) registerGRPCServices() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startHTTPServer() error {
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
mux := runtime.NewServeMux()
|
||||
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
|
||||
grpcEndpoint := fmt.Sprintf("localhost:%d", s.config.GRPCPort)
|
||||
|
||||
err := pb.RegisterCompetitionServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to register http handlers: %w", err)
|
||||
}
|
||||
|
||||
s.httpServer = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", s.config.HTTPPort),
|
||||
Handler: mux,
|
||||
ReadTimeout: httpReadTimeout,
|
||||
WriteTimeout: httpWriteTimeout,
|
||||
IdleTimeout: httpIdleTimeout,
|
||||
}
|
||||
|
||||
log.Printf("starting HTTP server on port %d", s.config.HTTPPort)
|
||||
return s.httpServer.ListenAndServe()
|
||||
}
|
||||
|
||||
func (s *Server) Stop() {
|
||||
log.Println("shutting down competition server...")
|
||||
|
||||
// Shutdown HTTP server
|
||||
if s.httpServer != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := s.httpServer.Shutdown(ctx); err != nil {
|
||||
log.Printf("failed to shutdown HTTP server gracefully: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown gRPC server
|
||||
if s.grpcServer != nil {
|
||||
s.grpcServer.GracefulStop()
|
||||
}
|
||||
|
||||
// Close database connection
|
||||
if s.db != nil {
|
||||
if err := s.db.Close(); err != nil {
|
||||
log.Printf("failed to close database connection: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user