Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/generic types #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/date-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ EXPOSE 8383

# Create a group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

# Tell docker that all future commands should run as the appuser user
USER appuser
Expand Down
1 change: 0 additions & 1 deletion src/finance-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ EXPOSE 8484

# Create a group and user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser

# Tell docker that all future commands should run as the appuser user
USER appuser
Expand Down
12 changes: 9 additions & 3 deletions src/finance-api/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ func InitializeZdjApi() (*zdj.ZdjApi, error) {
return &zdj.ZdjApi{}, nil
}

func InitializeMathApi() *mathematicals.MathApi {
wire.Build(mathematicals.ProvideMathApi, problem.NewMathService, problem.NewTwoGenerationService, repositories.MongoRepoSet, aws.ProvideAWSService, aws.AwsSet) //sql
func InitializeMathApi() *mathematicals.MathApi[int] {
wire.Build(mathematicals.ProvideMathApi, problem.NewMathService, problem.ProvideTwoGenerationService, repositories.MongoRepoSet, aws.ProvideAWSService, aws.AwsSet) //sql
//wire.Build(zdj.ProvideZdjApi, repository.MemoryRepoSet) //InMemory
return &mathematicals.MathApi{}
}

func InitializeMathApi2() *mathematicals.MathApi[float32] {
wire.Build(mathematicals.ProvideMathApi2, problem.NewMathService, problem.ProvideTwoGenerationService, repositories.MongoRepoSet, aws.ProvideAWSService, aws.AwsSet) //sql
//wire.Build(zdj.ProvideZdjApi, repository.MemoryRepoSet) //InMemory
return &mathematicals.MathApi{}
}
Expand All @@ -37,7 +43,7 @@ func InitializeMockApi() (*zdj.ZdjApi, error) {
}

func InitializeMockMathApi() *mathematicals.MathApi {
wire.Build(mathematicals.ProvideMathApi, problem.NewMathService, problem.NewTwoGenerationService, repositories.MongoRepoSet, aws.ProvideAWSService, aws.AwsMockSet) //sql
wire.Build(mathematicals.ProvideMathApi, problem.NewMathService, problem.ProvideTwoGenerationService2, repositories.MongoRepoSet, aws.ProvideAWSService, aws.AwsMockSet) //sql
//wire.Build(zdj.ProvideZdjApi, repository.MemoryRepoSet) //InMemory
return &mathematicals.MathApi{}
}
Expand Down
8 changes: 4 additions & 4 deletions src/finance-api/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/finance-api/financeApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func main() {

type ApiBoot struct {
ZdjApi *zdj.ZdjApi
MathApi *mathematicals.MathApi
MathApi *mathematicals.MathApi[int]
MathApiFloat *mathematicals.MathApi[float32]
ErrorHandler *middleware.ErrorHandlingMiddleware
AuthorizationHandler *middleware.AuthorizationMiddleware
Registry *mesh.Registry
Expand All @@ -46,8 +47,9 @@ func initialDependency() {
}

apiBoot = &ApiBoot{
ZdjApi: zdjApi,
MathApi: di.InitializeMathApi(),
ZdjApi: zdjApi,
MathApi: di.InitializeMathApi(),
//MathApiFloat: di.InitializeMathApi(),
AuthorizationHandler: di.InitialAuthorizationMiddleware(),
ErrorHandler: di.InitialErrorMiddleware(),
Registry: di.InitialRegistry(),
Expand Down Expand Up @@ -91,4 +93,12 @@ func defineRoutes(router *gin.Engine) {
mathGroupRouter.POST("/save", authorizationHandler, apiBoot.MathApi.SaveResults)
mathGroupRouter.POST("/multiple/feeds", apiBoot.MathApi.GetAllQuestionFeeds)
}

math2GroupRouter := router.Group("/homework/math2")
{
math2GroupRouter.POST("/", apiBoot.MathApi.GetQuestions)
math2GroupRouter.POST("/multiple", apiBoot.MathApi.GetAllQuestions)
math2GroupRouter.POST("/save", authorizationHandler, apiBoot.MathApi.SaveResults)
math2GroupRouter.POST("/multiple/feeds", apiBoot.MathApi.GetAllQuestionFeeds)
}
}
14 changes: 7 additions & 7 deletions src/finance-api/financeApi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func TestSearchAuthorized(t *testing.T) {

func TestGetQuestionsInvalid(t *testing.T) {
//Act
w := performRequest(router, "POST", "/homework/math/", problem.Criteria{Min: 10, Max: 20, Category: 1000})
w := performRequest(router, "POST", "/homework/math/", problem.Criteria[int]{Min: 10, Max: 20, Category: 1000})

var response []mathEntity.Problem
var response []mathEntity.Problem[int]
err := json.Unmarshal(w.Body.Bytes(), &response)

//Assert
Expand All @@ -81,7 +81,7 @@ func TestGetQuestionsInvalid(t *testing.T) {

func TestGetQuestions(t *testing.T) {
//Act
w := performRequest(router, "POST", "/homework/math/", problem.Criteria{
w := performRequest(router, "POST", "/homework/math/", problem.Criteria[int]{
Min: 10,
Max: 20,
Quantity: 100,
Expand All @@ -90,7 +90,7 @@ func TestGetQuestions(t *testing.T) {
Type: problem.TypePlainExpression,
})

var response problem.QuestionResponse
var response problem.QuestionResponse[int]
err := json.Unmarshal(w.Body.Bytes(), &response)

//Assert
Expand All @@ -101,7 +101,7 @@ func TestGetQuestions(t *testing.T) {

func TestGetQuestionsMultiple(t *testing.T) {
//Act
w := performRequest(router, "POST", "/homework/math/multiple", []problem.Criteria{
w := performRequest(router, "POST", "/homework/math/multiple", []problem.Criteria[int]{
{
Min: 10,
Max: 20,
Expand All @@ -112,7 +112,7 @@ func TestGetQuestionsMultiple(t *testing.T) {
},
})

var response problem.QuestionResponse
var response problem.QuestionResponse[int]
err := json.Unmarshal(w.Body.Bytes(), &response)

//Assert
Expand All @@ -126,7 +126,7 @@ func TestGetQuestionsMultiple(t *testing.T) {

func TestGetQuestionFeedsMultiple(t *testing.T) {
//Act
w := performRequest(router, "POST", "/homework/math/multiple/feeds", []problem.Criteria{
w := performRequest(router, "POST", "/homework/math/multiple/feeds", []problem.Criteria[int]{
{
Min: 10,
Max: 20,
Expand Down
56 changes: 48 additions & 8 deletions src/finance-api/mathematicals/di/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,68 @@
package di

import (
"reflect"

"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem/entity"
"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem/services"
"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem/services/data"
"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem/services/stratergy"
"github.com/google/wire"
)

func InitializeTwoPlusService() services.ProblemService {
wire.Build(services.ProblemServiceSet, stratergy.TwoPlusStratergySet, data.RandomServiceSet)
func InitializeTwoPlusService[number entity.Number]() services.ProblemService[number] {
var zero number = *new(number)
v := reflect.ValueOf(zero)

switch v.Kind() {
case reflect.Int:
wire.Build(services.ProblemServiceSet, stratergy.TwoPlusStratergySet, data.RandomServiceSet)
case reflect.Float32:
wire.Build(services.ProblemServiceSetFloat, stratergy.TwoPlusStratergySetFloat, data.RandomServiceSetFloat)
default:
panic("Invalid type")
}
return nil
}

func InitializeTwoMinusService() services.ProblemService {
wire.Build(services.ProblemServiceSet, stratergy.TwoMinusStratergySet, data.RandomServiceSet)
func InitializeTwoMinusService[number entity.Number]() services.ProblemService[number] {
var zero number = *new(number)
v := reflect.ValueOf(zero)
switch v.Kind() {
case reflect.Int:
wire.Build(services.ProblemServiceSet, stratergy.TwoMinusStratergySet, data.RandomServiceSet)
case reflect.Float32:
wire.Build(services.ProblemServiceSetFloat, stratergy.TwoMinusStratergySetFloat, data.RandomServiceSetFloat)
default:
panic("Invalid type")
}
return nil
}

func InitializeTwoMultiplyService() services.ProblemService {
wire.Build(services.ProblemServiceSet, stratergy.TwoMultiplyStratergySet, data.RandomServiceSet)
func InitializeTwoMultiplyService[number entity.Number]() services.ProblemService[number] {
var zero number = *new(number)
v := reflect.ValueOf(zero)
switch v.Kind() {
case reflect.Int:
wire.Build(services.ProblemServiceSet, stratergy.TwoMultiplyStratergySet, data.RandomServiceSet)
case reflect.Float32:
wire.Build(services.ProblemServiceSetFloat, stratergy.TwoMultiplyStratergySetFloat, data.RandomServiceSetFloat)
default:
panic("Invalid type")
}
return nil
}

func InitializeTwoDivideService() services.ProblemService {
wire.Build(services.ProblemServiceSet, stratergy.TwoDivideStratergySet, data.RandomServiceSet)
func InitializeTwoDivideService[number entity.Number]() services.ProblemService[number] {
var zero number = *new(number)
v := reflect.ValueOf(zero)
switch v.Kind() {
case reflect.Int:
wire.Build(services.ProblemServiceSet, stratergy.TwoDivideStratergySet, data.RandomServiceSet)
case reflect.Float32:
wire.Build(services.ProblemServiceSetFloat, stratergy.TwoDivideStratergySetFloat, data.RandomServiceSetFloat)
default:
panic("Invalid type")
}
return nil
}
43 changes: 0 additions & 43 deletions src/finance-api/mathematicals/di/wire_gen.go

This file was deleted.

29 changes: 17 additions & 12 deletions src/finance-api/mathematicals/mathApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ import (
"net/http"

"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem"
"github.com/FelixAnna/web-service-dlw/finance-api/mathematicals/problem/entity"
"github.com/gin-gonic/gin"
)

type MathApi struct {
mathService *problem.MathService
type MathApi[number entity.Number] struct {
mathService *problem.MathService[number]
}

//provide for wire
func ProvideMathApi(mathService *problem.MathService) *MathApi {
return &MathApi{mathService: mathService}
// provide for wire
func ProvideMathApi(mathService *problem.MathService[int]) *MathApi[int] {
return &MathApi[int]{mathService: mathService}
}

func (api *MathApi) GetQuestions(c *gin.Context) {
var criteria problem.Criteria
func ProvideMathApi2(mathService *problem.MathService[float32]) *MathApi[float32] {
return &MathApi[float32]{mathService: mathService}
}

func (api *MathApi[number]) GetQuestions(c *gin.Context) {
var criteria problem.Criteria[number]
if err := c.BindJSON(&criteria); err != nil {
log.Println(err)
c.String(http.StatusBadRequest, err.Error())
Expand All @@ -30,8 +35,8 @@ func (api *MathApi) GetQuestions(c *gin.Context) {
c.JSON(http.StatusOK, results)
}

func (api *MathApi) GetAllQuestions(c *gin.Context) {
var criterias []problem.Criteria
func (api *MathApi[number]) GetAllQuestions(c *gin.Context) {
var criterias []problem.Criteria[number]
if err := c.BindJSON(&criterias); err != nil {
log.Println(err)
c.String(http.StatusBadRequest, err.Error())
Expand All @@ -43,8 +48,8 @@ func (api *MathApi) GetAllQuestions(c *gin.Context) {
c.JSON(http.StatusOK, results)
}

func (api *MathApi) GetAllQuestionFeeds(c *gin.Context) {
var criterias []problem.Criteria
func (api *MathApi[number]) GetAllQuestionFeeds(c *gin.Context) {
var criterias []problem.Criteria[number]
if err := c.BindJSON(&criterias); err != nil {
log.Println(err)
c.String(http.StatusBadRequest, err.Error())
Expand All @@ -55,7 +60,7 @@ func (api *MathApi) GetAllQuestionFeeds(c *gin.Context) {

c.JSON(http.StatusOK, results)
}
func (api *MathApi) SaveResults(c *gin.Context) {
func (api *MathApi[number]) SaveResults(c *gin.Context) {
userId, _ := c.Get("userId")
var request problem.SaveAnswersRequest
if err := c.BindJSON(&request); err != nil {
Expand Down
Loading