discordwebhooks now bind to a user id. More work to do though

This commit is contained in:
James Tombleson 2024-05-04 11:58:35 -07:00
parent f0d36eb2ab
commit 53b0469647
4 changed files with 47 additions and 44 deletions

View File

@ -111,6 +111,13 @@ func (s *Handler) GetDiscordWebHooksByServerAndChannel(c echo.Context) error {
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse
func (s *Handler) NewDiscordWebHook(c echo.Context) error { func (s *Handler) NewDiscordWebHook(c echo.Context) error {
token, err := s.getJwtToken(c)
if err != nil {
return c.JSON(http.StatusUnauthorized, domain.BaseResponse{
Message: ErrJwtMissing,
})
}
_url := c.QueryParam("url") _url := c.QueryParam("url")
_server := c.QueryParam("server") _server := c.QueryParam("server")
_channel := c.QueryParam("channel") _channel := c.QueryParam("channel")
@ -136,7 +143,12 @@ func (s *Handler) NewDiscordWebHook(c echo.Context) error {
}) })
} }
rows, err := s.repo.DiscordWebHooks.Create(c.Request().Context(), _url, _server, _channel, true) user, err := s.repo.Users.GetByName(token.UserName)
if err != nil {
s.WriteMessage(c, ErrUserUnknown, http.StatusBadRequest)
}
rows, err := s.repo.DiscordWebHooks.Create(c.Request().Context(), user.ID, _url, _server, _channel, true)
if err != nil { if err != nil {
s.WriteError(c, err, http.StatusInternalServerError) s.WriteError(c, err, http.StatusInternalServerError)
} }

View File

@ -10,22 +10,11 @@ import (
"git.jamestombleson.com/jtom38/newsbot-api/internal/database" "git.jamestombleson.com/jtom38/newsbot-api/internal/database"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain" "git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services" "git.jamestombleson.com/jtom38/newsbot-api/internal/services"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
type ListSources struct {
ApiStatusModel
Payload []models.SourceDto `json:"payload"`
}
type GetSource struct {
ApiStatusModel
Payload models.SourceDto `json:"payload"`
}
// ListSources // ListSources
// @Summary Lists the top 50 records // @Summary Lists the top 50 records
// @Param page query string false "page number" // @Param page query string false "page number"
@ -35,7 +24,7 @@ type GetSource struct {
// @Success 200 {object} domain.SourcesResponse "ok" // @Success 200 {object} domain.SourcesResponse "ok"
// @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems" // @Failure 400 {object} domain.BaseResponse "Unable to reach SQL or Data problems"
func (s *Handler) listSources(c echo.Context) error { func (s *Handler) listSources(c echo.Context) error {
resp := domain.SourcesResponse { resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{ BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess, Message: ResponseMessageSuccess,
}, },
@ -437,7 +426,7 @@ func (s *Handler) deleteSources(c echo.Context) error {
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse
func (s *Handler) disableSource(c echo.Context) error { func (s *Handler) disableSource(c echo.Context) error {
resp := domain.SourcesResponse { resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{ BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess, Message: ResponseMessageSuccess,
}, },
@ -479,7 +468,7 @@ func (s *Handler) disableSource(c echo.Context) error {
// @Failure 400 {object} domain.BaseResponse // @Failure 400 {object} domain.BaseResponse
// @Failure 500 {object} domain.BaseResponse // @Failure 500 {object} domain.BaseResponse
func (s *Handler) enableSource(c echo.Context) error { func (s *Handler) enableSource(c echo.Context) error {
resp := domain.SourcesResponse { resp := domain.SourcesResponse{
BaseResponse: domain.BaseResponse{ BaseResponse: domain.BaseResponse{
Message: ResponseMessageSuccess, Message: ResponseMessageSuccess,
}, },

View File

@ -9,8 +9,8 @@ import (
"github.com/huandu/go-sqlbuilder" "github.com/huandu/go-sqlbuilder"
) )
type DiscordWebHookRepo interface{ type DiscordWebHookRepo interface {
Create(ctx context.Context, url, server, channel string, enabled bool) (int64, error) Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error)
Enable(ctx context.Context, id int64) (int64, error) Enable(ctx context.Context, id int64) (int64, error)
Disable(ctx context.Context, id int64) (int64, error) Disable(ctx context.Context, id int64) (int64, error)
SoftDelete(ctx context.Context, id int64) (int64, error) SoftDelete(ctx context.Context, id int64) (int64, error)
@ -32,12 +32,12 @@ func NewDiscordWebHookRepository(conn *sql.DB) discordWebHookRepository {
} }
} }
func (r discordWebHookRepository) Create(ctx context.Context, url, server, channel string, enabled bool) (int64, error) { func (r discordWebHookRepository) Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error) {
dt := time.Now() dt := time.Now()
queryBuilder := sqlbuilder.NewInsertBuilder() queryBuilder := sqlbuilder.NewInsertBuilder()
queryBuilder.InsertInto("DiscordWebHooks") queryBuilder.InsertInto("DiscordWebHooks")
queryBuilder.Cols("UpdatedAt", "CreatedAt", "DeletedAt", "Url", "Server", "Channel", "Enabled") queryBuilder.Cols("UpdatedAt", "CreatedAt", "DeletedAt", "UserID", "Url", "Server", "Channel", "Enabled")
queryBuilder.Values(dt, dt, timeZero, url, server, channel, enabled) queryBuilder.Values(dt, dt, timeZero, userId, url, server, channel, enabled)
query, args := queryBuilder.Build() query, args := queryBuilder.Build()
_, err := r.conn.ExecContext(ctx, query, args...) _, err := r.conn.ExecContext(ctx, query, args...)
@ -195,13 +195,14 @@ func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]domain.DiscordW
var createdAt time.Time var createdAt time.Time
var updatedAt time.Time var updatedAt time.Time
var deletedAt time.Time var deletedAt time.Time
var userId int64
var url string var url string
var server string var server string
var channel string var channel string
var enabled bool var enabled bool
err := rows.Scan( err := rows.Scan(
&id, &createdAt, &updatedAt, &id, &createdAt, &updatedAt,
&deletedAt, &url, &server, &deletedAt, &userId, &url, &server,
&channel, &enabled, &channel, &enabled,
) )
if err != nil { if err != nil {
@ -213,6 +214,7 @@ func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]domain.DiscordW
CreatedAt: createdAt, CreatedAt: createdAt,
UpdatedAt: updatedAt, UpdatedAt: updatedAt,
DeletedAt: deletedAt, DeletedAt: deletedAt,
UserID: userId,
Url: url, Url: url,
Server: server, Server: server,
Channel: channel, Channel: channel,

View File

@ -17,7 +17,7 @@ func TestCreateDiscordWebHookRecord(t *testing.T) {
defer db.Close() defer db.Close()
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
created, err := r.Create(context.Background(), "www.discord.com/bad/webhook", "Unit Testing", "memes", true) created, err := r.Create(context.Background(), 999, "www.discord.com/bad/webhook", "Unit Testing", "memes", true)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
t.FailNow() t.FailNow()
@ -38,7 +38,7 @@ func TestDiscordWebHookGetById(t *testing.T) {
defer db.Close() defer db.Close()
ctx := context.Background() ctx := context.Background()
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
created, err := r.Create(ctx, "www.discord.com/bad/webhook", "Unit Testing", "memes", true) created, err := r.Create(ctx, 999, "www.discord.com/bad/webhook", "Unit Testing", "memes", true)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
t.FailNow() t.FailNow()
@ -71,7 +71,7 @@ func TestDiscordWebHookGetByUrl(t *testing.T) {
ctx := context.Background() ctx := context.Background()
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", "Unit Testing", "memes", true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", "Unit Testing", "memes", true)
item, err := r.GetByUrl(ctx, "www.discord.com/bad/webhook") item, err := r.GetByUrl(ctx, "www.discord.com/bad/webhook")
if err != nil { if err != nil {
t.Log(err) t.Log(err)
@ -95,7 +95,7 @@ func TestDiscordWebHookListByServerName(t *testing.T) {
ctx := context.Background() ctx := context.Background()
serverName := "Unit Testing" serverName := "Unit Testing"
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, "memes", true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, "memes", true)
item, err := r.ListByServerName(ctx, serverName) item, err := r.ListByServerName(ctx, serverName)
if err != nil { if err != nil {
@ -121,7 +121,7 @@ func TestDiscordWebHookListByServerAndChannel(t *testing.T) {
serverName := "Unit Testing" serverName := "Unit Testing"
channel := "memes" channel := "memes"
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, channel, true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, channel, true)
item, err := r.ListByServerAndChannel(ctx, serverName, channel) item, err := r.ListByServerAndChannel(ctx, serverName, channel)
if err != nil { if err != nil {
@ -152,7 +152,7 @@ func TestDiscordWebHookEnableRecord(t *testing.T) {
serverName := "Unit Testing" serverName := "Unit Testing"
channel := "memes" channel := "memes"
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, channel, false) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, channel, false)
item, err := r.GetById(ctx, 1) item, err := r.GetById(ctx, 1)
if err != nil { if err != nil {
@ -195,7 +195,7 @@ func TestDiscordWebHookDisableRecord(t *testing.T) {
serverName := "Unit Testing" serverName := "Unit Testing"
channel := "memes" channel := "memes"
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, channel, true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, channel, true)
item, err := r.GetById(ctx, 1) item, err := r.GetById(ctx, 1)
if err != nil { if err != nil {
@ -238,7 +238,7 @@ func TestDiscordWebHookSoftDelete(t *testing.T) {
serverName := "Unit Testing" serverName := "Unit Testing"
channel := "memes" channel := "memes"
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, channel, true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, channel, true)
_, err = r.SoftDelete(ctx, 1) _, err = r.SoftDelete(ctx, 1)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
@ -263,7 +263,7 @@ func TestDiscordWebHookRestore(t *testing.T) {
timeZero := time.Time{} timeZero := time.Time{}
r := repository.NewDiscordWebHookRepository(db) r := repository.NewDiscordWebHookRepository(db)
_, _ = r.Create(ctx, "www.discord.com/bad/webhook", serverName, channel, true) _, _ = r.Create(ctx, 999, "www.discord.com/bad/webhook", serverName, channel, true)
item, _ := r.GetById(ctx, 1) item, _ := r.GetById(ctx, 1)
if item.DeletedAt != timeZero { if item.DeletedAt != timeZero {
t.Log("DeletedAt was not zero") t.Log("DeletedAt was not zero")