-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create API to fetch career page images and show them on career page
- Loading branch information
1 parent
33aa7b7
commit bec9e1a
Showing
10 changed files
with
1,005 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package career | ||
|
||
import ( | ||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
type LifeatCanopas struct { | ||
Id int `json:"id"` | ||
ImageUrls string `json:"imageUrls"` | ||
Index int `json:"index"` | ||
} | ||
|
||
type Perks struct { | ||
Id int `json:"id"` | ||
ImageUrls string `json:"imageUrls"` | ||
Index int `json:"index"` | ||
} | ||
|
||
type LifeRepository struct { | ||
Db *sqlx.DB | ||
} | ||
|
||
func New(db *sqlx.DB) *LifeRepository { | ||
return &LifeRepository{Db: db} | ||
} | ||
|
||
func (repository *LifeRepository) LifeImages(c *gin.Context) { | ||
lifeImages := []LifeatCanopas{} | ||
err := repository.Db.Select(&lifeImages, "SELECT l.id, l.imageUrls, l.index FROM lifeatcanopas l") | ||
if err != nil { | ||
log.Error(err) | ||
c.AbortWithStatus(http.StatusInternalServerError) | ||
return | ||
} | ||
c.JSON(http.StatusOK, lifeImages) | ||
} | ||
|
||
func (repository *LifeRepository) PerksImages(c *gin.Context) { | ||
perksImages := []Perks{} | ||
err := repository.Db.Select(&perksImages, "SELECT p.id, p.imageUrls, p.index FROM perks p") | ||
if err != nil { | ||
log.Error(err) | ||
c.AbortWithStatus(http.StatusInternalServerError) | ||
return | ||
} | ||
c.JSON(http.StatusOK, perksImages) | ||
} |
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,83 @@ | ||
package career | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"utils" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var repo *LifeRepository | ||
var err error | ||
var testDB *sqlx.DB | ||
|
||
func TestInit(t *testing.T) { | ||
testDB, err = utils.TestDB() | ||
if err != nil { | ||
t.Errorf("Error in initializing test DB: %v", err) | ||
} | ||
|
||
repo = New(testDB) | ||
} | ||
|
||
func TestGetAllLifeImages(t *testing.T) { | ||
utils.TruncateTables(testDB) | ||
err = utils.CreateTables(testDB) | ||
if err != nil { | ||
t.Errorf("Error in initializing test DB: %v", err) | ||
} | ||
utils.PrepareTablesData(testDB) | ||
engine := gin.New() | ||
engine.GET("/api/lifeimages", repo.LifeImages) | ||
req, err := http.NewRequest("GET", "/api/lifeimages", nil) | ||
if err != nil { | ||
t.Errorf("Error in creating request: %v", err) | ||
} | ||
w := httptest.NewRecorder() | ||
engine.ServeHTTP(w, req) | ||
|
||
assert.EqualValues(t, http.StatusOK, w.Code) | ||
got := utils.GotArrayData(w, t) | ||
expected := []interface{}{expectedLifeImages()} | ||
assert.Equal(t, expected, got) | ||
} | ||
|
||
func TestGetAllPerksImages(t *testing.T) { | ||
utils.TruncateTables(testDB) | ||
err = utils.CreateTables(testDB) | ||
if err != nil { | ||
t.Errorf("Error in initializing test DB: %v", err) | ||
} | ||
utils.PrepareTablesData(testDB) | ||
engine := gin.New() | ||
engine.GET("/api/perkimages", repo.PerksImages) | ||
req, err := http.NewRequest("GET", "/api/perkimages", nil) | ||
if err != nil { | ||
t.Errorf("Error in creating request: %v", err) | ||
} | ||
w := httptest.NewRecorder() | ||
engine.ServeHTTP(w, req) | ||
|
||
assert.EqualValues(t, http.StatusOK, w.Code) | ||
got := utils.GotArrayData(w, t) | ||
expected := []interface{}{expectedPerkImages()} | ||
assert.Equal(t, expected, got) | ||
} | ||
func expectedLifeImages() map[string]interface{} { | ||
lifeImages := make(map[string]interface{}) | ||
lifeImages["id"] = 1.0 | ||
lifeImages["imageUrls"] = "https://canopas-website.s3.ap-south-1.amazonaws.com/lifeCanopas/1-400.webp,https://canopas-website.s3.ap-south-1.amazonaws.com/lifeCanopas/1-800.webp,https://canopas-website.s3.ap-south-1.amazonaws.com/lifeCanopas/1-1600.webp" | ||
lifeImages["index"] = 0.0 | ||
return lifeImages | ||
} | ||
func expectedPerkImages() map[string]interface{} { | ||
perkImages := make(map[string]interface{}) | ||
perkImages["id"] = 1.0 | ||
perkImages["imageUrls"] = "https://canopas-website.s3.ap-south-1.amazonaws.com/lifeCanopas/1-400.webp,https://canopas-website.s3.ap-south-1.amazonaws.com/lifeCanopas/1-800.webp" | ||
perkImages["index"] = 0.0 | ||
return perkImages | ||
} |
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,63 @@ | ||
module career | ||
|
||
go 1.21.3 | ||
|
||
replace utils => ../utils | ||
|
||
replace db => ../db | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.47.2 | ||
github.com/gin-gonic/gin v1.9.1 | ||
github.com/jmoiron/sqlx v1.3.5 | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/stretchr/testify v1.8.3 | ||
) | ||
|
||
require ( | ||
cloud.google.com/go/compute v1.7.0 // indirect | ||
cloud.google.com/go/recaptchaenterprise v1.3.1 // indirect | ||
cloud.google.com/go/recaptchaenterprise/v2 v2.0.1 // indirect | ||
db v0.0.0-00010101000000-000000000000 // indirect | ||
github.com/bytedance/sonic v1.9.1 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // 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/go-sql-driver/mysql v1.6.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/go-cmp v0.5.8 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect | ||
github.com/googleapis/gax-go/v2 v2.4.0 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.4 // 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/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.11 // indirect | ||
go.opencensus.io v0.23.0 // indirect | ||
golang.org/x/arch v0.3.0 // indirect | ||
golang.org/x/crypto v0.14.0 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
google.golang.org/api v0.94.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/genproto v0.0.0-20220902135211-223410557253 // indirect | ||
google.golang.org/grpc v1.48.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
utils v0.0.0-00010101000000-000000000000 // indirect | ||
) |
Oops, something went wrong.