-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
140 additions
and
14 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
DB=postgres://postgres:admin@localhost:5434/racket?sslmode=disable | ||
AUTH0_DOMAIN= | ||
AUTH0_CLIENTID= | ||
AUTH0_CLIENTSECRET= | ||
AUTH0_CLIENTSECRET= | ||
RESEND_API_KEY=re_YuDJPgda_KNvkxxiTAgTvL76JtYpi3qU8 |
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,5 @@ | ||
package dto | ||
|
||
type EmailDto struct { | ||
To []string `json:"to"` | ||
} |
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
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,29 +1,33 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/truc9/racket/domain" | ||
"github.com/truc9/racket/dto" | ||
"github.com/truc9/racket/params" | ||
"github.com/truc9/racket/service/email" | ||
"go.uber.org/zap" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type PlayerHandler struct { | ||
db *gorm.DB | ||
logger *zap.SugaredLogger | ||
db *gorm.DB | ||
logger *zap.SugaredLogger | ||
emailer email.Emailer | ||
} | ||
|
||
func NewPlayerHandler(db *gorm.DB, logger *zap.SugaredLogger) *PlayerHandler { | ||
func NewPlayerHandler(db *gorm.DB, logger *zap.SugaredLogger, emailer email.Emailer) *PlayerHandler { | ||
return &PlayerHandler{ | ||
db: db, | ||
logger: logger, | ||
db: db, | ||
logger: logger, | ||
emailer: emailer, | ||
} | ||
} | ||
|
||
func (h PlayerHandler) Create(c *gin.Context) { | ||
func (h *PlayerHandler) Create(c *gin.Context) { | ||
dto := dto.PlayerDto{} | ||
var err error | ||
if err = c.BindJSON(&dto); err != nil { | ||
|
@@ -39,13 +43,13 @@ func (h PlayerHandler) Create(c *gin.Context) { | |
c.JSON(http.StatusCreated, p) | ||
} | ||
|
||
func (h PlayerHandler) GetAll(c *gin.Context) { | ||
func (h *PlayerHandler) GetAll(c *gin.Context) { | ||
var result []domain.Player | ||
h.db.Order("first_name ASC").Find(&result) | ||
c.JSON(http.StatusOK, result) | ||
} | ||
|
||
func (h PlayerHandler) GetExternalUserAttendantRequests(c *gin.Context) { | ||
func (h *PlayerHandler) GetExternalUserAttendantRequests(c *gin.Context) { | ||
var result []dto.PlayerAttendantRequestDto | ||
externalUserId := params.Get(c, "externalUserId") | ||
h.db.Raw(` | ||
|
@@ -61,7 +65,7 @@ func (h PlayerHandler) GetExternalUserAttendantRequests(c *gin.Context) { | |
c.JSON(http.StatusOK, result) | ||
} | ||
|
||
func (h PlayerHandler) Update(c *gin.Context) { | ||
func (h *PlayerHandler) Update(c *gin.Context) { | ||
var model dto.PlayerDto | ||
id := params.Get(c, "playerId") | ||
if err := c.BindJSON(&model); err != nil { | ||
|
@@ -77,8 +81,23 @@ func (h PlayerHandler) Update(c *gin.Context) { | |
c.JSON(http.StatusOK, p) | ||
} | ||
|
||
func (h PlayerHandler) Delete(c *gin.Context) { | ||
func (h *PlayerHandler) Delete(c *gin.Context) { | ||
id := params.Get(c, "playerId") | ||
h.db.Unscoped().Delete(&domain.Player{}, id) | ||
c.Status(http.StatusOK) | ||
} | ||
|
||
func (h *PlayerHandler) SendWelcomeEmail(c *gin.Context) { | ||
dto := &dto.EmailDto{} | ||
c.BindJSON(dto) | ||
|
||
playerId := params.Get(c, "playerId") | ||
var name string | ||
h.db.Find(&domain.Player{}, playerId).Select("first_name", &name) | ||
|
||
_, err := h.emailer.Send("[email protected]", dto.To, "Welcome", fmt.Sprintf("<h1>Welcome %s to Racket!</h1>", name)) | ||
if err != nil { | ||
c.AbortWithError(http.StatusInternalServerError, err) | ||
} | ||
c.Status(http.StatusOK) | ||
} |
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,5 @@ | ||
package email | ||
|
||
type Emailer interface { | ||
Send(from string, to []string, subject, body string) (*EmailerRes, error) | ||
} |
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,5 @@ | ||
package email | ||
|
||
type EmailerRes struct { | ||
Id string `json:"id"` | ||
} |
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,47 @@ | ||
package email | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
resend "github.com/resend/resend-go/v2" | ||
) | ||
|
||
type ResendEmailer struct { | ||
} | ||
|
||
func NewResendEmailer() Emailer { | ||
return &ResendEmailer{} | ||
} | ||
|
||
func (re *ResendEmailer) Send(from string, to []string, subject, body string) (*EmailerRes, error) { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
|
||
API_KEY := os.Getenv("RESEND_API_KEY") | ||
|
||
client := resend.NewClient(API_KEY) | ||
|
||
params := &resend.SendEmailRequest{ | ||
From: "The Racket <[email protected]>", | ||
To: to, | ||
Html: body, | ||
Subject: subject, | ||
} | ||
|
||
sent, err := client.Emails.Send(params) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return nil, err | ||
} | ||
|
||
fmt.Println(sent.Id) | ||
|
||
return &EmailerRes{ | ||
Id: sent.Id, | ||
}, err | ||
} |