-
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.
- Loading branch information
Showing
23 changed files
with
1,398 additions
and
293 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package views | ||
package commonviews | ||
|
||
import ( | ||
"encoding/json" | ||
|
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,4 +1,4 @@ | ||
package views | ||
package commonviews | ||
|
||
import "net/http" | ||
|
||
|
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 @@ | ||
DROP TABLE IF EXISTS pets; |
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,17 @@ | ||
CREATE TABLE IF NOT EXISTS pets ( | ||
id SERIAL PRIMARY KEY, | ||
owner_id BIGINT NOT NULL, | ||
name VARCHAR(30) NOT NULL, | ||
pet_type VARCHAR(10) NOT NULL, | ||
sex VARCHAR(10) NOT NULL, | ||
neutered BOOLEAN NOT NULL, | ||
breed VARCHAR(30) NOT NULL, | ||
birth_date DATE NOT NULL, | ||
weight_in_kg DECIMAL(5, 2) NOT NULL, | ||
additional_note VARCHAR(500), | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP | ||
); | ||
|
||
CREATE INDEX pets_owner_id_idx ON pets (owner_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
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,93 @@ | ||
package database | ||
|
||
import "github.com/pet-sitter/pets-next-door-api/internal/models" | ||
|
||
func (tx *Tx) CreatePet(pet *models.Pet) (*models.Pet, error) { | ||
err := tx.QueryRow(` | ||
INSERT INTO | ||
pets | ||
( | ||
owner_id, | ||
name, | ||
pet_type, | ||
sex, | ||
neutered, | ||
breed, | ||
birth_date, | ||
weight_in_kg, | ||
created_at, | ||
updated_at | ||
) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW()) | ||
RETURNING id, created_at, updated_at | ||
`, | ||
pet.OwnerID, | ||
pet.Name, | ||
pet.PetType, | ||
pet.Sex, | ||
pet.Neutered, | ||
pet.Breed, | ||
pet.BirthDate, | ||
pet.WeightInKg, | ||
).Scan(&pet.ID, &pet.CreatedAt, &pet.UpdatedAt) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pet, nil | ||
} | ||
|
||
func (tx *Tx) FindPetsByOwnerID(ownerID int) ([]models.Pet, error) { | ||
var pets []models.Pet | ||
|
||
rows, err := tx.Query(` | ||
SELECT | ||
id, | ||
owner_id, | ||
name, | ||
pet_type, | ||
sex, | ||
neutered, | ||
breed, | ||
birth_date, | ||
weight_in_kg, | ||
created_at, | ||
updated_at | ||
FROM | ||
pets | ||
WHERE | ||
owner_id = $1 AND | ||
deleted_at IS NULL | ||
`, | ||
ownerID, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var pet models.Pet | ||
|
||
if err := rows.Scan( | ||
&pet.ID, | ||
&pet.OwnerID, | ||
&pet.Name, | ||
&pet.PetType, | ||
&pet.Sex, | ||
&pet.Neutered, | ||
&pet.Breed, | ||
&pet.BirthDate, | ||
&pet.WeightInKg, | ||
&pet.CreatedAt, | ||
&pet.UpdatedAt, | ||
); err != nil { | ||
return nil, err | ||
} | ||
|
||
pets = append(pets, pet) | ||
} | ||
|
||
return pets, 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package models | ||
|
||
import "github.com/pet-sitter/pets-next-door-api/internal/types" | ||
|
||
type Pet struct { | ||
ID int `field:"id"` | ||
OwnerID int `field:"owner_id"` | ||
Name string `field:"name"` | ||
PetType types.PetType `field:"pet_type"` | ||
Sex types.PetSex `field:"sex"` | ||
Neutered bool `field:"neutered"` | ||
Breed string `field:"breed"` | ||
BirthDate string `field:"birth_date"` | ||
WeightInKg float64 `field:"weight_in_kg"` | ||
CreatedAt string `field:"created_at"` | ||
UpdatedAt string `field:"updated_at"` | ||
DeletedAt string `field:"deleted_at"` | ||
} |
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.