Update usages of Title to use Filename
parent
ba367555e1
commit
9cec9fd3e1
2
go.mod
2
go.mod
|
@ -16,7 +16,7 @@ require (
|
|||
github.com/lib/pq v1.0.0
|
||||
github.com/markbates/deplist v1.0.5 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.10.0 // indirect
|
||||
github.com/pressly/goose v2.4.5+incompatible // indirect
|
||||
github.com/pressly/goose v2.4.5+incompatible
|
||||
github.com/sirupsen/logrus v1.4.0
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/ziutek/mymysql v1.5.4 // indirect
|
||||
|
|
|
@ -49,9 +49,9 @@ func (file *MockFile) Close() error {
|
|||
|
||||
func setupPaster() Paster {
|
||||
paste := repository.Paste{
|
||||
ID: 23,
|
||||
Handle: uuid.Nil,
|
||||
Title: "Test post",
|
||||
ID: 23,
|
||||
Handle: uuid.Nil,
|
||||
Filename: "Test post",
|
||||
}
|
||||
|
||||
return Paster{
|
||||
|
|
|
@ -17,8 +17,8 @@ func NewService() *Service {
|
|||
}
|
||||
|
||||
// CreatePaste makes a new paste to be stored
|
||||
func (service Service) CreatePaste(title string) (Paster, error) {
|
||||
paste, err := service.db.PutPaste(title)
|
||||
func (service Service) CreatePaste(filename string) (Paster, error) {
|
||||
paste, err := service.db.PutPaste(filename)
|
||||
if err != nil {
|
||||
return Paster{}, err
|
||||
}
|
||||
|
|
|
@ -19,8 +19,8 @@ func (repo *MockPasteRepository) GetPaste(handle uuid.UUID) (repository.Paste, e
|
|||
return args.Get(0).(repository.Paste), args.Error(1)
|
||||
}
|
||||
|
||||
func (repo *MockPasteRepository) PutPaste(title string) (repository.Paste, error) {
|
||||
args := repo.Called(title)
|
||||
func (repo *MockPasteRepository) PutPaste(filename string) (repository.Paste, error) {
|
||||
args := repo.Called(filename)
|
||||
|
||||
return args.Get(0).(repository.Paste), args.Error(1)
|
||||
}
|
||||
|
@ -43,14 +43,14 @@ func TestCreatePaste(t *testing.T) {
|
|||
service := setupService()
|
||||
mockRepo := service.db.(*MockPasteRepository)
|
||||
testPaste := repository.Paste{
|
||||
ID: 23,
|
||||
Title: "My Awesome Paste",
|
||||
Handle: uuid.New(),
|
||||
ID: 23,
|
||||
Filename: "My Awesome Paste",
|
||||
Handle: uuid.New(),
|
||||
}
|
||||
|
||||
mockRepo.On("PutPaste", testPaste.Title).Return(testPaste, nil).Once()
|
||||
mockRepo.On("PutPaste", testPaste.Filename).Return(testPaste, nil).Once()
|
||||
|
||||
paster, err := service.CreatePaste(testPaste.Title)
|
||||
paster, err := service.CreatePaste(testPaste.Filename)
|
||||
mockRepo.AssertExpectations(t)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, testPaste, paster.Paste)
|
||||
|
@ -61,9 +61,9 @@ func TestGetPaste(t *testing.T) {
|
|||
service := setupService()
|
||||
mockRepo := service.db.(*MockPasteRepository)
|
||||
testPaste := repository.Paste{
|
||||
ID: 23,
|
||||
Title: "My Awesome Paste",
|
||||
Handle: uuid.New(),
|
||||
ID: 23,
|
||||
Filename: "My Awesome Paste",
|
||||
Handle: uuid.New(),
|
||||
}
|
||||
|
||||
mockRepo.On("GetPaste", testPaste.Handle).Return(testPaste, nil).Once()
|
||||
|
|
|
@ -4,9 +4,9 @@ import "github.com/google/uuid"
|
|||
|
||||
// Paste represents a paste that the user has stored in the database
|
||||
type Paste struct {
|
||||
ID int
|
||||
Title string
|
||||
Handle uuid.UUID
|
||||
ID int
|
||||
Filename string
|
||||
Handle uuid.UUID
|
||||
}
|
||||
|
||||
//PasteRepository puts/gets a paste from the database
|
||||
|
@ -14,7 +14,7 @@ type PasteRepository interface {
|
|||
// GetPaste should get a paste with the given handle
|
||||
GetPaste(handle uuid.UUID) (Paste, error)
|
||||
// PutPaste should make a new paste and return the generated uuid
|
||||
PutPaste(title string) (Paste, error)
|
||||
PutPaste(filename string) (Paste, error)
|
||||
// UpdatePaste should update the paste in the database, preserving the id
|
||||
UpdatePaste(paste Paste) error
|
||||
}
|
||||
|
@ -24,17 +24,17 @@ func (db *DatabaseConnector) GetPaste(handle uuid.UUID) (Paste, error) {
|
|||
row := db.DB.QueryRow("SELECT * FROM paste WHERE handle=$1", handle)
|
||||
paste := Paste{}
|
||||
|
||||
return paste, row.Scan(&paste.ID, &paste.Title, &paste.Handle)
|
||||
return paste, row.Scan(&paste.ID, &paste.Filename, &paste.Handle)
|
||||
}
|
||||
|
||||
// PutPaste puts a paste in the database
|
||||
func (db *DatabaseConnector) PutPaste(title string) (Paste, error) {
|
||||
func (db *DatabaseConnector) PutPaste(filename string) (Paste, error) {
|
||||
handle, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return Paste{}, err
|
||||
}
|
||||
|
||||
insertResult := db.DB.QueryRow("INSERT INTO paste VALUES(DEFAULT, $1, $2) RETURNING pasteID", title, handle.String())
|
||||
insertResult := db.DB.QueryRow("INSERT INTO paste VALUES(DEFAULT, $1, $2) RETURNING pasteID", filename, handle.String())
|
||||
|
||||
var pasteID int
|
||||
err = insertResult.Scan(&pasteID)
|
||||
|
@ -44,16 +44,16 @@ func (db *DatabaseConnector) PutPaste(title string) (Paste, error) {
|
|||
|
||||
return Paste{
|
||||
// Even though pasteID is an int64, Postgres' SERIAL is 32 bits, and int is defined to be >= 32 bits
|
||||
ID: int(pasteID),
|
||||
Title: title,
|
||||
Handle: handle,
|
||||
ID: int(pasteID),
|
||||
Filename: filename,
|
||||
Handle: handle,
|
||||
}, err
|
||||
}
|
||||
|
||||
// UpdatePaste allows for a paste to be updated.
|
||||
// Currently, only the title can be updated
|
||||
// Currently, only the filename can be updated
|
||||
func (db *DatabaseConnector) UpdatePaste(paste Paste) error {
|
||||
_, err := db.DB.Exec("UPDATE paste SET title = $1 WHERE pasteID = $2", paste.Title, paste.ID)
|
||||
_, err := db.DB.Exec("UPDATE paste SET filename = $1 WHERE pasteID = $2", paste.Filename, paste.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue