Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: decouple business logic from db logic #90

Merged
merged 16 commits into from
Mar 9, 2022

Conversation

whoAbhishekSah
Copy link
Member

Refactor packages to abstract out repositories into Store for

  • Alerts
  • Templates
  • Providers
  • Receivers
  • Namespaces

@@ -73,13 +72,14 @@ func RunServer(c *domain.Config) error {
return err
}

store, err := store.New(&c.DB)
gormDB, err := store.New(&c.DB)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whoAbhishekSah this can simply be called db.

)

type NamespaceRepository interface {
Migrate() error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whoAbhishekSah Let's remove migrate from repositories and we can have a migrate method that takes care of overall database migration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel repository being the single interface for all DB-related actions, it should have the Migrate method, so that caller can use it. Otherwise, the caller will have to know the DB details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrate is a bootstrap process which is something services will not use. Check out Guardian ref. Only database implementation should know about migration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this one while refactoring each package one by one.

cmd/alert.go Outdated
@@ -94,9 +94,9 @@ func listAlertsCmd(c *configuration) *cobra.Command {
}

cmd.Flags().StringVar(&providerName, "provider-name", "", "provider name")
cmd.MarkFlagRequired("provider-name")
_ = cmd.MarkFlagRequired("provider-name")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when we weren't using the _ = assignment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't check what happened, it was added as part of linting issues.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can disable this linting rule if needed. But lets avoid adding it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whoAbhishekSah lets avoid this change.

Comment on lines +11 to +15
List() ([]*model.Namespace, error)
Create(*model.Namespace) (*model.Namespace, error)
Get(uint64) (*model.Namespace, error)
Update(*model.Namespace) (*model.Namespace, error)
Delete(uint64) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repository should accept and return *domain.Namespace instead of *model.Namespace so we don't have to use namespaceModel.ToDomain(), namespaceModel.FromDomain() or any other model operations inside service. It should be inside the repository only.
plus, when it's only used inside repository, ToDomain() and FromDomain() could be private methods as well

@@ -1,14 +1,13 @@
package alerts
package model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can get rid this model package and have the model definitions inside store/postgres package all together, since the model definitions are specific to postgres and/or gorm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi we're going to do the same for guardian

@whoAbhishekSah whoAbhishekSah changed the title Refactor/decouple business logic from db logic refactor: decouple business logic from db logic Feb 16, 2022
@whoAbhishekSah whoAbhishekSah force-pushed the refactor/decouple_business_logic_from_db_logic branch from 8c3cf5d to a61957d Compare March 3, 2022 09:42
@ravisuhag ravisuhag merged commit f259af8 into main Mar 9, 2022
@ravisuhag ravisuhag deleted the refactor/decouple_business_logic_from_db_logic branch March 9, 2022 07:31
ravisuhag pushed a commit that referenced this pull request Aug 21, 2022
* refactor: rename metric to telemetry

* refactor: rename handler v1 to v1beta1

* refactor: rename helper to utils

* build: add golangci-lint in CI pipeline and fix linting issues

* refactor: remove redundant codeexchange package

* refactor: cleanup go.mod file

* refactor: move provider repository to postgres store

* refactor: move namespace repository to postgres store

* refactor: move templates repository to postgres store

* refactor: move receiver repository to postgres store

* refactor: move all repository interface definitions in one file

* refactor: remove dependency from gorm in services by injecting repositories

* refactor: move alerts repository to postgres store

* refactor: move slack notifier client to provider package

* refactor: update provider repository to operate on domain definition

* chore: remove errcheck for CLI flags and disable linting rule
ravisuhag pushed a commit that referenced this pull request Aug 21, 2022
* refactor: rename metric to telemetry

* refactor: rename handler v1 to v1beta1

* refactor: rename helper to utils

* build: add golangci-lint in CI pipeline and fix linting issues

* refactor: remove redundant codeexchange package

* refactor: cleanup go.mod file

* refactor: move provider repository to postgres store

* refactor: move namespace repository to postgres store

* refactor: move templates repository to postgres store

* refactor: move receiver repository to postgres store

* refactor: move all repository interface definitions in one file

* refactor: remove dependency from gorm in services by injecting repositories

* refactor: move alerts repository to postgres store

* refactor: move slack notifier client to provider package

* refactor: update provider repository to operate on domain definition

* chore: remove errcheck for CLI flags and disable linting rule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants