Skip to content

Commit

Permalink
refactor: seperate mock and memory db
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Dec 29, 2021
1 parent 7c6713e commit 03ca4e3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 88 deletions.
2 changes: 1 addition & 1 deletion app/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type MessageServicePort interface {
type MessageRepositoryPort interface {
CreateMessage(msg *Message) error
GetMessage(uuid string) (*Message, error)
UpdateMessage(uuid string, updateFN func(msg *Message) (*Message, error)) (*Message, error)
UpdateMessage(msg *Message, updateFN func(msg *Message) (*Message, error)) (*Message, error)
}

type AttachmentRepositoryPort interface {
Expand Down
2 changes: 1 addition & 1 deletion right/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (db *DB) GetMessage(uuid string) (*app.Message, error) {
return nil, app.ErrNotImplemented
}

func (db *DB) UpdateMessage(uuid string, updateFN func(msg *app.Message) (*app.Message, error)) (*app.Message, error) {
func (db *DB) UpdateMessage(msg *app.Message, updateFN func(msg *app.Message) (*app.Message, error)) (*app.Message, error) {
return nil, app.ErrNotImplemented
}

Expand Down
108 changes: 108 additions & 0 deletions right/database/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package database

import (
"sync"

"github.com/ItsNotGoodName/smtpbridge/app"
)

type Memory struct {
attMu sync.RWMutex
att map[string][]app.Attachment
msgMu sync.RWMutex
msg map[string]app.Message
}

func NewMemory() *Memory {
return &Memory{
attMu: sync.RWMutex{},
att: make(map[string][]app.Attachment),
msgMu: sync.RWMutex{},
msg: make(map[string]app.Message),
}
}

func (db *Memory) CreateMessage(msg *app.Message) error {
db.msgMu.Lock()
defer db.msgMu.Unlock()
if _, ok := db.msg[msg.UUID]; ok {
return app.ErrMessageAlreadyExists
}

db.msg[msg.UUID] = *msg

return nil
}

func (db *Memory) UpdateMessage(msg *app.Message, updateFN func(msg *app.Message) (*app.Message, error)) (*app.Message, error) {
db.msgMu.Lock()
defer db.msgMu.Unlock()

dbMSG, ok := db.msg[msg.UUID]
if !ok {
return nil, app.ErrMessageNotFound
}

updatedMsg, err := updateFN(&dbMSG)
if err != nil {
return nil, err
}

db.msg[msg.UUID] = *updatedMsg

return updatedMsg, nil
}

func (db *Memory) GetAttachment(uuid string) (*app.Attachment, error) {
return nil, app.ErrNotImplemented
}

func (db *Memory) GetMessage(uuid string) (*app.Message, error) {
db.msgMu.RLock()
msg := db.msg[uuid]
db.msgMu.RUnlock()
atts, err := db.GetAttachmentsByMessage(&msg)
if err != nil {
return nil, err
}

msg.Attachments = atts

return &msg, nil
}

func (db *Memory) CreateAttachment(att *app.Attachment) error {
db.attMu.Lock()
atts, ok := db.att[att.MessageUUID]
if !ok {
atts = []app.Attachment{}
}

atts = append(atts, *att)
db.att[att.MessageUUID] = atts
db.attMu.Unlock()

return nil
}

func (db *Memory) LoadAttachment(msg *app.Message) error {
atts, err := db.GetAttachmentsByMessage(msg)
if err != nil {
return err
}

msg.Attachments = atts

return nil
}

func (db *Memory) GetAttachmentsByMessage(message *app.Message) ([]app.Attachment, error) {
db.attMu.RLock()
atts, ok := db.att[message.UUID]
if !ok {
atts = []app.Attachment{}
}
db.attMu.RUnlock()

return atts, nil
}
95 changes: 10 additions & 85 deletions right/database/mock.go
Original file line number Diff line number Diff line change
@@ -1,108 +1,33 @@
package database

import (
"sync"
import "github.com/ItsNotGoodName/smtpbridge/app"

"github.com/ItsNotGoodName/smtpbridge/app"
)

type Mock struct {
attMu sync.RWMutex
att map[string][]app.Attachment
msgMu sync.RWMutex
msg map[string]app.Message
}
type Mock struct{}

func NewMock() *Mock {
return &Mock{
attMu: sync.RWMutex{},
att: make(map[string][]app.Attachment),
msgMu: sync.RWMutex{},
msg: make(map[string]app.Message),
}
return &Mock{}
}

func (db *Mock) CreateMessage(msg *app.Message) error {
db.msgMu.Lock()
defer db.msgMu.Unlock()
if _, ok := db.msg[msg.UUID]; ok {
return app.ErrMessageAlreadyExists
}

db.msg[msg.UUID] = *msg

return nil
}

func (db *Mock) UpdateMessage(uuid string, updateFN func(msg *app.Message) (*app.Message, error)) (*app.Message, error) {
db.msgMu.Lock()
defer db.msgMu.Unlock()

msg, ok := db.msg[uuid]
if !ok {
return nil, app.ErrMessageNotFound
}

updatedMsg, err := updateFN(&msg)
if err != nil {
return nil, err
}

db.msg[uuid] = *updatedMsg

return updatedMsg, nil
}

func (db *Mock) GetAttachment(uuid string) (*app.Attachment, error) {
func (db *Mock) GetMessage(uuid string) (*app.Message, error) {
return nil, app.ErrNotImplemented
}

func (db *Mock) GetMessage(uuid string) (*app.Message, error) {
db.msgMu.RLock()
msg := db.msg[uuid]
db.msgMu.RUnlock()
atts, err := db.GetAttachmentsByMessage(&msg)
if err != nil {
return nil, err
}

msg.Attachments = atts

return &msg, nil
func (db *Mock) UpdateMessage(msg *app.Message, updateFN func(msg *app.Message) (*app.Message, error)) (*app.Message, error) {
return updateFN(msg)
}

func (db *Mock) CreateAttachment(att *app.Attachment) error {
db.attMu.Lock()
atts, ok := db.att[att.MessageUUID]
if !ok {
atts = []app.Attachment{}
}

atts = append(atts, *att)
db.att[att.MessageUUID] = atts
db.attMu.Unlock()

return nil
}

func (db *Mock) LoadAttachment(msg *app.Message) error {
atts, err := db.GetAttachmentsByMessage(msg)
if err != nil {
return err
}

msg.Attachments = atts

return nil
func (db *Mock) GetAttachment(uuid string) (*app.Attachment, error) {
return nil, app.ErrNotImplemented
}

func (db *Mock) GetAttachmentsByMessage(message *app.Message) ([]app.Attachment, error) {
db.attMu.RLock()
atts, ok := db.att[message.UUID]
if !ok {
atts = []app.Attachment{}
}
db.attMu.RUnlock()

return atts, nil
func (db *Mock) LoadAttachment(msg *app.Message) error {
return app.ErrNotImplemented
}
2 changes: 1 addition & 1 deletion service/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *Message) CreateAttachment(msg *app.Message, name string, data []byte) (
}

func (m *Message) UpdateStatus(msg *app.Message, status app.Status) error {
_, err := m.messageREPO.UpdateMessage(msg.UUID, func(msg *app.Message) (*app.Message, error) {
_, err := m.messageREPO.UpdateMessage(msg, func(msg *app.Message) (*app.Message, error) {
msg.Status = status
return msg, nil
})
Expand Down

0 comments on commit 03ca4e3

Please sign in to comment.