Skip to content

Instantly share code, notes, and snippets.

View vedashree29296's full-sized avatar

Vedashree Patil vedashree29296

View GitHub Profile
@vedashree29296
vedashree29296 / getone-category.go
Created February 9, 2021 05:59
Fetch one row based on id
//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:
//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
//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
//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 {
@vedashree29296
vedashree29296 / category.go
Created January 30, 2021 04:50
Category model
//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 {
@vedashree29296
vedashree29296 / postgres-init.go
Created January 29, 2021 08:47
Connecting to Postgres
package postgres
import (
"database/sql"
"fmt"
//for connecting to db
_ "github.com/lib/pq"
)
const (
@vedashree29296
vedashree29296 / petstore-db-v1.sql
Created January 26, 2021 06:11
database sql file for petstore app in Go Chronicles
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")
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
@vedashree29296
vedashree29296 / petstore-oas-post-simple-v1.yaml
Last active January 25, 2021 10:59
Example for POST call
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:
@vedashree29296
vedashree29296 / oas-schema-example.yaml
Last active January 25, 2021 10:48
Example to write a schema in OAS
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: