-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add POST Apply subscription API
- Loading branch information
Showing
24 changed files
with
898 additions
and
18 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,16 @@ | ||
```bash | ||
go run ./cmd/server . | ||
|
||
# "google/wire" module | ||
go install github.com/google/wire/cmd/wire@latest | ||
wire ./cmd/server | ||
|
||
# "swaggo/swag" module | ||
go install github.com/swaggo/swag/cmd/swag@latest | ||
swag init -pd -g ./cmd/server/main.go -o ./third_party/docs/ | ||
|
||
# "golang.org/x/tools" module | ||
go install golang.org/x/tools/cmd/goimports@latest | ||
goimports -w . | ||
gofmt -s -w . | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
"LOG_LEVEL": 4 | ||
}, | ||
"SWAGGER": { | ||
"HOST": "localhost", | ||
"HOST": "localhost:5000", | ||
"BASE_PATH": "/" | ||
} | ||
} |
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,72 @@ | ||
package repository | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/team-nerd-planet/api-server/infra/database" | ||
"github.com/team-nerd-planet/api-server/internal/entity" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type SubscriptionRepo struct { | ||
db *database.Database | ||
} | ||
|
||
func NewSubscriptionRepo(db *database.Database) entity.SubscriptionRepo { | ||
db.AutoMigrate(&entity.Subscription{}) | ||
|
||
return &SubscriptionRepo{ | ||
db: db, | ||
} | ||
} | ||
|
||
// Create implements entity.SubscriptionRepo. | ||
func (sr *SubscriptionRepo) Create(newSubscription entity.Subscription) (*entity.Subscription, error) { | ||
err := sr.db.Create(&newSubscription).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &newSubscription, nil | ||
} | ||
|
||
// Delete implements entity.SubscriptionRepo. | ||
func (sr *SubscriptionRepo) Delete(id int64) (*entity.Subscription, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
// ExistEmail implements entity.SubscriptionRepo. | ||
func (sr *SubscriptionRepo) ExistEmail(email string) (*int64, error) { | ||
data := entity.Subscription{} | ||
|
||
err := sr.db.Select("id").Take(&data, "email = ?", email).Error | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
|
||
id := int64(data.ID) | ||
return &id, nil | ||
} | ||
|
||
// Update implements entity.SubscriptionRepo. | ||
func (sr *SubscriptionRepo) Update(id int64, newSubscription entity.Subscription) (*entity.Subscription, error) { | ||
var ( | ||
subscription entity.Subscription | ||
) | ||
|
||
err := sr.db.First(&subscription, id).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subscription = newSubscription | ||
subscription.ID = uint(id) | ||
err = sr.db.Save(&subscription).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &subscription, nil | ||
} |
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
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,59 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/team-nerd-planet/api-server/infra/router/util" | ||
"github.com/team-nerd-planet/api-server/internal/controller/rest" | ||
"github.com/team-nerd-planet/api-server/internal/controller/rest/dto/subscription_dto" | ||
) | ||
|
||
// Apply | ||
// | ||
// @Summary Apply subscription | ||
// @Description apply for subscription | ||
// @Tags subscription | ||
// @Schemes http | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body subscription_dto.ApplyReq true "contents for applying for subscription." | ||
// @Success 200 {object} subscription_dto.ApplyRes | ||
// @Failure 400 {object} util.HTTPError | ||
// @Failure 500 {object} util.HTTPError | ||
// @Router /v1/subscription/apply [post] | ||
func Apply(c *gin.Context, ctrl rest.SubscriptionController) { | ||
req, err := util.ValidateBody[subscription_dto.ApplyReq](c) | ||
if err != nil { | ||
util.NewError(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
res, ok := ctrl.Apply(*req) | ||
if !ok { | ||
util.NewError(c, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, res) | ||
} | ||
|
||
func Approve(c *gin.Context, ctrl rest.SubscriptionController) { | ||
req, err := util.ValidateQuery[subscription_dto.ApproveReq](c) | ||
if err != nil { | ||
c.Redirect(http.StatusBadRequest, "https://www.nerdplanet.app") | ||
return | ||
} | ||
|
||
res, ok := ctrl.Approve(*req) | ||
if !ok { | ||
c.Redirect(http.StatusInternalServerError, "https://www.nerdplanet.app") | ||
return | ||
} | ||
|
||
if res.Ok { | ||
c.Redirect(http.StatusFound, "https://www.nerdplanet.app") | ||
} else { | ||
c.Redirect(http.StatusBadRequest, "https://www.nerdplanet.app") | ||
} | ||
} |
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
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
internal/controller/rest/dto/subscription_dto/apply_dto.go
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,34 @@ | ||
package subscription_dto | ||
|
||
import "github.com/team-nerd-planet/api-server/internal/entity" | ||
|
||
type ApplyReq struct { | ||
Email string `json:"email" binding:"required,email"` // 이메일 | ||
Name *string `json:"name" binding:"omitempty"` // 이름 | ||
Division *string `json:"division" binding:"omitempty"` // 소속 | ||
PreferredCompanyArr []int64 `json:"preferred_company_arr" binding:"required"` // 회사 DB ID 배열 | ||
PreferredCompanySizeArr []entity.CompanySizeType `json:"preferred_companySize_arr" binding:"required"` // 회사 규모 배열 (0:스타트업, 1:중소기업, 2:중견기업, 3:대기업, 4:외국계) | ||
PreferredJobArr []int64 `json:"preferred_job_arr" binding:"required"` // 직무 DB ID 배열 | ||
PreferredSkillArr []int64 `json:"preferred_skill_arr" binding:"required"` // 스킬 DB ID 배열 | ||
} | ||
|
||
type ApplyRes struct { | ||
Ok bool `json:"ok"` // 구독 신청 메일 전송 결과 | ||
} | ||
|
||
func (asr ApplyReq) NewSubscription() entity.Subscription { | ||
companySizeArr := make([]int64, len(asr.PreferredCompanySizeArr)) | ||
for i, companySize := range asr.PreferredCompanySizeArr { | ||
companySizeArr[i] = int64(companySize) | ||
} | ||
|
||
return entity.Subscription{ | ||
Email: asr.Email, | ||
Name: asr.Name, | ||
Division: asr.Division, | ||
PreferredCompanyArr: asr.PreferredCompanyArr, | ||
PreferredCompanySizeArr: companySizeArr, | ||
PreferredJobArr: asr.PreferredJobArr, | ||
PreferredSkillArr: asr.PreferredSkillArr, | ||
} | ||
} |
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,9 @@ | ||
package subscription_dto | ||
|
||
type ApproveReq struct { | ||
Token string `form:"token" binding:"required"` | ||
} | ||
|
||
type ApproveRes struct { | ||
Ok bool `json:"ok"` // 구독 인증 결과 | ||
} |
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,48 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/team-nerd-planet/api-server/internal/controller/rest/dto/subscription_dto" | ||
"github.com/team-nerd-planet/api-server/internal/usecase/subscription" | ||
) | ||
|
||
type SubscriptionController struct { | ||
subscriptionUcase subscription.SubscriptionUsecase | ||
} | ||
|
||
func NewSubscriptionController(subscriptionUcase subscription.SubscriptionUsecase) SubscriptionController { | ||
return SubscriptionController{ | ||
subscriptionUcase: subscriptionUcase, | ||
} | ||
} | ||
|
||
func (sc SubscriptionController) Apply(req subscription_dto.ApplyReq) (*subscription_dto.ApplyRes, bool) { | ||
subscription, ok := sc.subscriptionUcase.ApplySubscription(req.NewSubscription()) | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
result := false | ||
if subscription != nil { | ||
result = true | ||
} | ||
|
||
return &subscription_dto.ApplyRes{ | ||
Ok: result, | ||
}, true | ||
} | ||
|
||
func (sc SubscriptionController) Approve(req subscription_dto.ApproveReq) (*subscription_dto.ApproveRes, bool) { | ||
subscription, ok := sc.subscriptionUcase.Subscribe(req.Token) | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
result := false | ||
if subscription != nil { | ||
result = true | ||
} | ||
|
||
return &subscription_dto.ApproveRes{ | ||
Ok: result, | ||
}, true | ||
} |
Oops, something went wrong.