-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(githooks): git diff check * refactor(all): add tests for InMemoryMechanismRepositorySpec and InMemoryReactionRepositorySpec
- Loading branch information
1 parent
56fa116
commit 5200066
Showing
11 changed files
with
291 additions
and
69 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
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
65 changes: 34 additions & 31 deletions
65
src/main/scala/core/repositories/InMemoryReactionRepository.scala
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 |
---|---|---|
@@ -1,80 +1,83 @@ | ||
package core.repositories | ||
|
||
import cats.effect.{Ref, Sync} | ||
import cats.implicits.toFunctorOps | ||
import cats.syntax.all._ | ||
|
||
import core.domain.preprocessor.{Reaction, ReactionId} | ||
import types.ReactionRepository | ||
import core.errors.http.preprocessor.ReactionError | ||
import core.errors.http.preprocessor.ReactionError.CreationError | ||
|
||
import types.ReactionRepository | ||
|
||
/** | ||
* An in-memory implementation of the `ReactionRepository` for testing and local use. | ||
* An in-memory implementation of `ReactionRepository` for testing and local use. | ||
* | ||
* @param state | ||
* A reference to a mutable map representing the current state of stored reactions. | ||
* A `Ref` encapsulating the mutable map of reactions. | ||
* @tparam F | ||
* The effect type (e.g., `IO`, `SyncIO`, etc.). | ||
* Effect type, such as `IO` or `SyncIO`. | ||
*/ | ||
class InMemoryReactionRepository[F[_]: Sync](state: Ref[F, Map[ReactionId, Reaction]]) | ||
class FunctionalInMemoryReactionRepository[F[_]: Sync](state: Ref[F, Map[ReactionId, Reaction]]) | ||
extends ReactionRepository[F] { | ||
|
||
/** | ||
* Generates a new unique ID for a reaction. | ||
* | ||
* @param currentState | ||
* @param reactions | ||
* The current state of the stored reactions. | ||
* @return | ||
* The next available integer ID. | ||
*/ | ||
private def generateId(currentState: Map[ReactionId, Reaction]): Int = | ||
currentState.keys.maxOption.getOrElse(0) + 1 | ||
private def generateId(reactions: Map[ReactionId, Reaction]): ReactionId = | ||
reactions.keys.maxOption.fold(1)(_ + 1) | ||
|
||
/** | ||
* Retrieves a reaction by its ID. | ||
* | ||
* @param id | ||
* The ID of the reaction to retrieve. | ||
* @return | ||
* An effect wrapping an optional `Reaction`. Returns `None` if the reaction is not found. | ||
* Effectful optional `Reaction`. Returns `None` if not found. | ||
*/ | ||
def get(id: ReactionId): F[Option[Reaction]] = | ||
state.get.map(_.get(id)) | ||
|
||
/** | ||
* Creates a new reaction and stores it in the repository. | ||
* Creates a new reaction. | ||
* | ||
* @param reaction | ||
* The `Reaction` instance to create. | ||
* The `Reaction` instance to add. | ||
* @return | ||
* An effect wrapping either a `ReactionError` if the creation fails or the created `Reaction` on success. | ||
* - If a reaction with the same name already exists, returns a `CreationError`. | ||
* Effectful result of `Either` with an error or the new reaction. | ||
*/ | ||
def create(reaction: Reaction): F[Either[ReactionError, Reaction]] = { | ||
state.modify { reactions => | ||
val id = generateId(reactions) | ||
if (reactions.values.exists(_.reactionName == reaction.reactionName)) { | ||
(reactions, Left(CreationError(s"Reaction with name '${reaction.reactionName}' already exists"))) | ||
} else { | ||
val newReaction = reaction.copy(id) | ||
(reactions + (id -> newReaction), Right(newReaction)) | ||
} | ||
def create(reaction: Reaction): F[Either[ReactionError, Reaction]] = | ||
state.get.flatMap { reactions => | ||
reactions.values | ||
.find(_.reactionName === reaction.reactionName) | ||
.fold { | ||
val newId = generateId(reactions) | ||
val newReaction = reaction.copy(newId) | ||
state.update(_ + (newId -> newReaction)).as(newReaction.asRight[ReactionError]) | ||
} { _ => | ||
CreationError(s"Reaction with name '${reaction.reactionName}' already exists") | ||
.asLeft[Reaction] | ||
.pure[F] | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Deletes a reaction by its ID. | ||
* | ||
* @param id | ||
* The ID of the reaction to delete. | ||
* @return | ||
* An effect wrapping a `Boolean` indicating whether the deletion was successful. | ||
* - Returns `true` if the reaction was successfully deleted. | ||
* - Returns `false` if the reaction ID was not found. | ||
* Effectful `Boolean` indicating if the deletion was successful. | ||
*/ | ||
def delete(id: ReactionId): F[Boolean] = | ||
state.modify { reactions => | ||
if (reactions.contains(id)) (reactions - id, true) | ||
else (reactions, false) | ||
} | ||
state.modify(reactions => | ||
reactions | ||
.get(id) | ||
.fold((reactions, false))(_ => (reactions - id, true)) | ||
) | ||
|
||
} |
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
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
7 changes: 5 additions & 2 deletions
7
src/main/scala/core/services/preprocessor/ReactionService.scala
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
Oops, something went wrong.