-
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.
refactor: seperate mock and memory db
- Loading branch information
1 parent
7c6713e
commit 03ca4e3
Showing
5 changed files
with
121 additions
and
88 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
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 | ||
} |
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,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 | ||
} |
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