Skip to content

Commit

Permalink
Refactor application services and repositories for improved structure…
Browse files Browse the repository at this point in the history
… and clarity
  • Loading branch information
AchimGrolimund committed Dec 14, 2024
1 parent 018cbf9 commit 85fa130
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 200 deletions.
2 changes: 1 addition & 1 deletion cmd/csp-scout-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
}

// Create service
service := application.NewReportService(repo)
service := application.NewService(repo)

// Initialize Gin router
router := gin.Default()
Expand Down
26 changes: 26 additions & 0 deletions pkg/application/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package application

import (
"context"
)

// Repository defines the complete repository interface combining all sub-repositories
type Repository interface {
ReportsRepository
StatisticsRepository
Close(ctx context.Context) error
}

// Service defines the complete service interface combining all sub-services
type Service struct {
Reports ReportsService
Statistics StatisticsService
}

// NewService creates a new complete service instance
func NewService(repo Repository) *Service {
return &Service{
Reports: NewReportsService(repo),
Statistics: NewStatisticsService(repo),
}
}
56 changes: 0 additions & 56 deletions pkg/application/report_service.go

This file was deleted.

43 changes: 43 additions & 0 deletions pkg/application/reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package application

import (
"context"

"github.com/AchimGrolimund/CSP-Scout-API/pkg/domain"
)

// ReportsRepository defines reports-specific repository methods
type ReportsRepository interface {
CreateReport(ctx context.Context, report *domain.Report) error
GetReport(ctx context.Context, id string) (*domain.Report, error)
ListReports(ctx context.Context) ([]domain.Report, error)
}

// ReportsService defines reports-specific service methods
type ReportsService interface {
CreateReport(ctx context.Context, report *domain.Report) error
GetReport(ctx context.Context, id string) (*domain.Report, error)
ListReports(ctx context.Context) ([]domain.Report, error)
}

type reportsService struct {
repo ReportsRepository
}

func NewReportsService(repo ReportsRepository) ReportsService {
return &reportsService{
repo: repo,
}
}

func (s *reportsService) CreateReport(ctx context.Context, report *domain.Report) error {
return s.repo.CreateReport(ctx, report)
}

func (s *reportsService) GetReport(ctx context.Context, id string) (*domain.Report, error) {
return s.repo.GetReport(ctx, id)
}

func (s *reportsService) ListReports(ctx context.Context) ([]domain.Report, error) {
return s.repo.ListReports(ctx)
}
47 changes: 47 additions & 0 deletions pkg/application/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package application

import (
"context"
)

// TopIPResult represents a client IP with its occurrence count
type TopIPResult struct {
IP string `json:"ip"`
Count int `json:"count"`
}

// TopDirectiveResult represents a violated directive with its occurrence count
type TopDirectiveResult struct {
Directive string `json:"directive"`
Count int `json:"count"`
}

// StatisticsRepository defines statistics-specific repository methods
type StatisticsRepository interface {
GetTopIPs(ctx context.Context) ([]TopIPResult, error)
GetTopViolatedDirectives(ctx context.Context) ([]TopDirectiveResult, error)
}

// StatisticsService defines statistics-specific service methods
type StatisticsService interface {
GetTopIPs(ctx context.Context) ([]TopIPResult, error)
GetTopViolatedDirectives(ctx context.Context) ([]TopDirectiveResult, error)
}

type statisticsService struct {
repo StatisticsRepository
}

func NewStatisticsService(repo StatisticsRepository) StatisticsService {
return &statisticsService{
repo: repo,
}
}

func (s *statisticsService) GetTopIPs(ctx context.Context) ([]TopIPResult, error) {
return s.repo.GetTopIPs(ctx)
}

func (s *statisticsService) GetTopViolatedDirectives(ctx context.Context) ([]TopDirectiveResult, error) {
return s.repo.GetTopViolatedDirectives(ctx)
}
39 changes: 39 additions & 0 deletions pkg/infrastructure/mongodb/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package mongodb

import (
"context"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// MongoRepository implements the application.Repository interface
type MongoRepository struct {
client *mongo.Client
database string
collection string
}

// NewMongoRepository creates a new MongoDB repository instance
func NewMongoRepository(uri, database, collection string) (*MongoRepository, error) {
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri))
if err != nil {
return nil, err
}

return &MongoRepository{
client: client,
database: database,
collection: collection,
}, nil
}

// Close implements the Close method required by the Repository interface
func (r *MongoRepository) Close(ctx context.Context) error {
return r.client.Disconnect(ctx)
}

// getCollection returns the MongoDB collection
func (r *MongoRepository) getCollection() *mongo.Collection {
return r.client.Database(r.database).Collection(r.collection)
}
47 changes: 47 additions & 0 deletions pkg/infrastructure/mongodb/reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package mongodb

import (
"context"

"github.com/AchimGrolimund/CSP-Scout-API/pkg/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

// CreateReport implements ReportsRepository.CreateReport
func (r *MongoRepository) CreateReport(ctx context.Context, report *domain.Report) error {
_, err := r.getCollection().InsertOne(ctx, report)
return err
}

// GetReport implements ReportsRepository.GetReport
func (r *MongoRepository) GetReport(ctx context.Context, id string) (*domain.Report, error) {
objectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}

var report domain.Report
err = r.getCollection().FindOne(ctx, bson.M{"_id": objectID}).Decode(&report)
if err != nil {
return nil, err
}

return &report, nil
}

// ListReports implements ReportsRepository.ListReports
func (r *MongoRepository) ListReports(ctx context.Context) ([]domain.Report, error) {
cursor, err := r.getCollection().Find(ctx, bson.M{})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)

var reports []domain.Report
if err := cursor.All(ctx, &reports); err != nil {
return nil, err
}

return reports, nil
}
Loading

0 comments on commit 85fa130

Please sign in to comment.