-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from k-avy/kavya/api/task1
feat: added GET endpoints Reviewed by: [email protected]
- Loading branch information
Showing
6 changed files
with
230 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ laas | |
|
||
# Go workspace file | ||
go.work | ||
licenseRef.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
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,18 +1,43 @@ | ||
module github.com/fossology/LicenseDb | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/gin-gonic/gin v1.9.1 | ||
gorm.io/driver/postgres v1.5.2 | ||
gorm.io/gorm v1.25.1 | ||
) | ||
|
||
require ( | ||
github.com/bytedance/sonic v1.9.1 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.14.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jackc/pgx/v5 v5.3.1 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
golang.org/x/crypto v0.8.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/leodido/go-urn v1.2.4 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect | ||
github.com/rogpeppe/go-internal v1.10.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.11 // indirect | ||
golang.org/x/arch v0.3.0 // indirect | ||
golang.org/x/crypto v0.9.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/sys v0.8.0 // indirect | ||
golang.org/x/text v0.9.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,69 @@ | ||
// SPDX-FileCopyrightText: 2023 Kavya Shukla <[email protected]> | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
|
||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/fossology/LicenseDb/pkg/models" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func GetAllLicense(c *gin.Context) { | ||
var licenses []models.License | ||
|
||
err := DB.Find(&licenses).Error | ||
if err != nil { | ||
er := models.LicenseError{ | ||
Status: http.StatusBadRequest, | ||
Message: "Licenses not found", | ||
Error: err.Error(), | ||
Path: "/api/licenses", | ||
Timestamp: time.Now().Format(time.RFC3339), | ||
} | ||
c.JSON(http.StatusBadRequest, er) | ||
return | ||
} | ||
res := models.LicenseResponse{ | ||
Data: licenses, | ||
Status: http.StatusOK, | ||
} | ||
|
||
c.JSON(http.StatusOK, res) | ||
} | ||
|
||
func GetLicense(c *gin.Context) { | ||
var license models.License | ||
|
||
queryParam := c.Param("shortname") | ||
if queryParam == "" { | ||
return | ||
} | ||
|
||
err := DB.Where("shortname = ?", queryParam).First(&license).Error | ||
|
||
if err != nil { | ||
er := models.LicenseError{ | ||
Status: http.StatusBadRequest, | ||
Message: fmt.Sprintf("no license with shortname '%s' exists", queryParam), | ||
Error: err.Error(), | ||
Path: fmt.Sprintf("/api/license/%s", queryParam), | ||
Timestamp: time.Now().Format(time.RFC3339), | ||
} | ||
c.JSON(http.StatusBadRequest, er) | ||
return | ||
} | ||
|
||
res := models.LicenseResponse{ | ||
Data: []models.License{license}, | ||
Status: http.StatusOK, | ||
} | ||
|
||
c.JSON(http.StatusOK, res) | ||
} |
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