@@ -7,36 +7,68 @@ import (
77
88 "github.com/hydrocode-de/datailama/internal/db"
99 "github.com/hydrocode-de/datailama/internal/sql"
10+ "github.com/hydrocode-de/datailama/internal/version"
1011)
1112
13+ // VersionOutput defines the response structure for the version endpoint
14+ type VersionOutput struct {
15+ Body struct {
16+ Version string `json:"version" example:"1.0.0" doc:"The version of the application"`
17+ BuildTime string `json:"build_time,omitempty" doc:"The build time of the application"`
18+ GitCommit string `json:"git_commit,omitempty" doc:"The git commit of the application"`
19+ }
20+ }
21+
22+ // TitleSearchOutput defines the response structure for the paper search endpoint
1223type TitleSearchOutput struct {
1324 Body struct {
1425 Count int `json:"count"`
1526 Paper []sql.SearchPaperByTitleRow `json:"paper"`
1627 }
1728}
1829
30+ // getVersion handles the version endpoint
31+ func getVersion (ctx context.Context , input * struct {}) (* VersionOutput , error ) {
32+ resp := & VersionOutput {}
33+ resp .Body .Version = version .Version
34+ resp .Body .BuildTime = version .BuildTime
35+ resp .Body .GitCommit = version .GitCommit
36+ return resp , nil
37+ }
38+
39+ // searchByTitle handles the paper search endpoint
1940func searchByTitle (ctx context.Context , input * struct {
2041 Title string `query:"title,omitempty" doc:"The title to search for"`
2142 Author string `query:"author,omitempty" doc:"The author to limit the search to"`
2243 OrderBy string `query:"order,omitempty" example:"citations_year" doc:"The property to order the results by. Can be citations_year or citations"`
2344 Direction string `query:"direction,omitempty" example:"desc" doc:"The direction to order the results by. Can be asc or desc"`
2445}) (* TitleSearchOutput , error ) {
25- if strings .ToLower (input .OrderBy ) != "citations_year" && strings .ToLower (input .OrderBy ) != "citations" {
46+ if strings .ToLower (input .OrderBy ) != "citations_year" && strings .ToLower (input .OrderBy ) != "citations" && input . OrderBy != "" {
2647 return nil , fmt .Errorf ("invalid order by argument: %v. Has to be citations_year or citations" , input .OrderBy )
2748 }
2849
29- if strings .ToLower (input .Direction ) != "asc" && strings .ToLower (input .Direction ) != "desc" {
50+ if strings .ToLower (input .Direction ) != "asc" && strings .ToLower (input .Direction ) != "desc" && input . Direction != "" {
3051 return nil , fmt .Errorf ("invalid direction argument: %v. Has to be asc or desc" , input .Direction )
3152 }
3253
54+ // Set defaults
55+ orderBy := input .OrderBy
56+ if orderBy == "" {
57+ orderBy = "citations_year"
58+ }
59+
60+ direction := input .Direction
61+ if direction == "" {
62+ direction = "desc"
63+ }
64+
3365 db := ctx .Value ("db" ).(* db.Manager )
3466
3567 papers , err := db .SearchPaperByTitle (ctx , sql.SearchPaperByTitleParams {
3668 Title : input .Title ,
3769 Author : input .Author ,
38- OrderBy : input . OrderBy ,
39- Direction : input . Direction ,
70+ OrderBy : orderBy ,
71+ Direction : direction ,
4072 Limit : 15 ,
4173 })
4274 if err != nil {
0 commit comments