-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor application services and repositories for improved structure…
… and clarity
- Loading branch information
1 parent
018cbf9
commit 85fa130
Showing
12 changed files
with
287 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.