-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change architecture to CQRS event sourcing
- Loading branch information
Paul Baecher
committed
Feb 22, 2018
1 parent
a038885
commit e347004
Showing
16 changed files
with
507 additions
and
567 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,64 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/DataDog/datadog-go/statsd" | ||
"github.com/MEDIGO/laika/models" | ||
"github.com/MEDIGO/laika/notifier" | ||
"github.com/MEDIGO/laika/store" | ||
"github.com/labstack/echo" | ||
) | ||
|
||
type EventResource struct { | ||
store store.Store | ||
stats *statsd.Client | ||
notifier notifier.Notifier | ||
} | ||
|
||
func NewEventResource(store store.Store, stats *statsd.Client, notifier notifier.Notifier) *EventResource { | ||
return &EventResource{store, stats, notifier} | ||
} | ||
|
||
func (r *EventResource) Create(c echo.Context) error { | ||
eventType := c.Param("type") | ||
event, err := models.EventForType(eventType) | ||
if err != nil { | ||
return Invalid(c, err.Error()) | ||
} | ||
|
||
if err := c.Bind(&event); err != nil { | ||
return BadRequest(c, "Body must be a valid JSON object") | ||
} | ||
|
||
state, err := r.store.State() | ||
if err != nil { | ||
return InternalServerError(c, err) | ||
} | ||
|
||
valErr, err := event.Validate(state) | ||
if err != nil { | ||
return InternalServerError(c, err) | ||
} else if valErr != nil { | ||
return BadRequest(c, valErr.Error()) | ||
} | ||
|
||
event, err = event.PrePersist(state) | ||
if err != nil { | ||
return InternalServerError(c, err) | ||
} | ||
|
||
cleanEvent, err := json.Marshal(event) | ||
if err != nil { | ||
return InternalServerError(c, err) | ||
} | ||
|
||
id, err := r.store.Persist(eventType, string(cleanEvent)) | ||
if err != nil { | ||
return InternalServerError(c, err) | ||
} | ||
|
||
return Created(c, struct { | ||
ID int64 `json:"id"` | ||
}{ID: id}) | ||
} |
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
Oops, something went wrong.