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
//GetCategory : get one Category based on an id in the DB | |
func (c Category) GetCategory(id int) (*models.Category, error) { | |
// create and execute query to return one row | |
row := client.DbClient.QueryRow("SELECT * from category WHERE id=$1", id) | |
switch err := row.Scan(&c.ID, &c.CategoryName); err { | |
// if no matching records found | |
case sql.ErrNoRows: | |
return nil, errors.New("No records found") | |
// success case | |
case 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
//Location : model for location | |
type Location struct { | |
ID int `json:"id"` | |
LocationName string `json:"locationName"` | |
} | |
//LocationService : interface for location model | |
type LocationService interface { | |
// add a new location | |
CreateLocation() error |
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
//Pet : model for a pet | |
type Pet struct { | |
ID int `json:"id"` | |
Name string `json:"name"` | |
Age float64 `json:"age"` | |
Description string `json:"description"` | |
ImageURL string `json:"imageUrl"` | |
PetLocation Location `json:"location"` // refers to the "Location" struct defined earlies | |
PetCategory Category `json:"category"` // refers to the "Category" struct defined earlies |
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
//Breed : model for deifining a breed of a pet | |
type Breed struct { | |
ID int `json:"breedId"` | |
BreedName string `json:"breedName"` | |
CategoryID int `json:"categoryId"` // refers to the id for a category of pet | |
} | |
//BreedService : interface for breed deifining operations on breed | |
type BreedService interface { | |
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
//Category : Model defining category of a pet | |
type Category struct { | |
ID int `json:"id"` | |
CategoryName string `json:"categoryname"` | |
} | |
//CategoryService : interface for Category model that defines the operations on it | |
type CategoryService interface { |
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
package postgres | |
import ( | |
"database/sql" | |
"fmt" | |
//for connecting to db | |
_ "github.com/lib/pq" | |
) | |
const ( |
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
CREATE TABLE "category" ( | |
"id" int PRIMARY KEY, | |
"category_name" varchar | |
); | |
CREATE TABLE "breed" ( | |
"id" int, | |
"breed_name" varchar, | |
"category_id" int, | |
PRIMARY KEY ("id", "category_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
paths: | |
"/pet": | |
get: | |
parameters: | |
- in: query # defines the query parameter that should be sent which is the category id | |
name: categoryId | |
schema: | |
type: integer | |
example: 1 | |
required: true |
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
paths: # all routes go under this | |
"/pet": | |
post: # define the type ie get/post/put/delete | |
tags: | |
- Pet | |
summary: Add a new pet to the store | |
requestBody: # define the request body under this | |
content: | |
application/json: # this shows that the request body is a JSON object | |
schema: |
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
components: # define schemas under this section | |
schemas: | |
Category: # create a category schema | |
description: Category of pets | |
type: object # this can correspond to a JSON object | |
properties: # define the properties in the object | |
id: | |
type: integer # define the type of object | |
example: 1 # give an example for better documentation | |
categoryName: |