api

package
v0.0.0-...-83f815e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	VOTES_QUERY = fmt.Sprintf(`
  select   votes.answer,
           count(case votes.vote when %d then 1 else null end) as upvotes,
           count(case votes.vote when %d then 1 else null end) as downvotes
  from     votes
  where deleted_at is NULL
  group by answer
`, VoteUp, VoteDown)
	ANSWERS_QUERY = fmt.Sprintf(`
  select   *
  from     answers
  full join     (%s) on answer = answers.id
  where    deleted_at is NULL and answers.parent is NULL and answers.question = ? 
`, VOTES_QUERY)
	REPLIES_QUERY = `
  select   *
  from     answers
  where    deleted_at is NULL and answers.parent IN ?
`
)

Functions

func DelAnswerHandler

func DelAnswerHandler(res http.ResponseWriter, req *http.Request)

@Summary Delete an answer @Description Given an andwer ID, delete the answer @Tags answer @Param id path string true "Answer id" @Produce json @Success 200 {object} Answer @Failure 400 {object} util.ApiError @Router /answers/{id} [delete]

func GetDocumentHandler

func GetDocumentHandler(res http.ResponseWriter, req *http.Request)

@Summary Get a document's divisions @Description Given a document's ID, return all the questions @Tags document @Param id path string true "document id" @Produce json @Success 200 {object} Document @Failure 400 {object} util.ApiError @Router /documents/{id} [get]

func GetQuestionHandler

func GetQuestionHandler(res http.ResponseWriter, req *http.Request)

@Summary Get all answers given a question @Description Given a question ID, return the question and all its answers @Tags question @Param id path string true "Answer id" @Produce json @Success 200 {array} Answer @Failure 400 {object} util.ApiError @Router /questions/{id} [get]

func GetUserVote

func GetUserVote(res http.ResponseWriter, req *http.Request)

get given vote to an answer

func PostVote

func PostVote(res http.ResponseWriter, req *http.Request)

@Summary Insert a vote @Description Insert a new vote on a answer @Tags vote @Produce json @Param id path string true "code query parameter" @Success 200 {object} Vote @Failure 400 {object} httputil.ApiError @Router /answer/{id}/vote [post]

func PutAnswerHandler

func PutAnswerHandler(res http.ResponseWriter, req *http.Request)

@Summary Insert a new answer @Description Insert a new answer under a question @Tags answer @Param answerReq body PutAnswerRequest true "Answer data to insert" @Produce json @Success 200 {object} Answer @Failure 400 {object} util.ApiError @Router /answers [put]

func PutDocumentHandler

func PutDocumentHandler(res http.ResponseWriter, req *http.Request)

@Summary Insert a new document @Description Insert a new document with all the questions initialised @Tags document @Param docRequest body PutDocumentRequest true "Doc request body" @Produce json @Success 200 {object} Document @Failure 400 {object} util.ApiError @Router /documents [put]

Types

type Answer

type Answer struct {
	// taken from from gorm.Model, so we can json strigify properly
	ID        uint           `json:"id" gorm:"primarykey"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`

	Question uint  `json:"question" gorm:"foreignKey:Question;references:ID"`
	Parent   *uint `json:"parent"`

	User      string   `json:"user"`
	Content   string   `json:"content"`
	Upvotes   uint32   `json:"upvotes" gorm:"->"`
	Downvotes uint32   `json:"downvotes" gorm:"->"`
	Replies   []Answer `json:"replies" gorm:"foreignKey:Parent;references:ID"`
	Votes     []Vote   `json:"-" gorm:"foreignKey:Answer;references:ID"`
}

type Coord

type Coord struct {
	Start uint32 `json:"start"`
	End   uint32 `json:"end"`
}

type Document

type Document struct {
	ID        string     `json:"id"`
	Questions []Question `json:"questions"`
}

type PutAnswerRequest

type PutAnswerRequest struct {
	Question uint   `json:"question"`
	Parent   *uint  `json:"parent"`
	Content  string `json:"content"`
}

type PutDocumentRequest

type PutDocumentRequest struct {
	ID     string  `json:"id"`
	Coords []Coord `json:"coords"`
}

type PutVoteRequest

type PutVoteRequest struct {
	Vote VoteValue `json:"vote"`
}

type Question

type Question struct {
	// taken from from gorm.Model, so we can json strigify properly
	ID        uint           `json:"id" gorm:"primarykey"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`

	Document string   `json:"document"`
	Start    uint32   `json:"start"`
	End      uint32   `json:"end"`
	Answers  []Answer `json:"answers" gorm:"foreignKey:Question;references:ID"`
}

type Vote

type Vote struct {
	Answer uint   `json:"answer" gorm:"primaryKey"`
	User   string `json:"user" gorm:"primaryKey"`
	Vote   int8   `json:"vote"`

	// taken from from gorm.Model
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

type VoteValue

type VoteValue int8
const (
	VoteUp   VoteValue = 1
	VoteNone VoteValue = 0
	VoteDown VoteValue = -1
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳