Skip to content

Commit

Permalink
feat: HandleDashboardUserPostLogin API
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatanvesh committed Aug 15, 2023
1 parent 8567e79 commit 1621729
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .idea/sqldialects.xml

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

3 changes: 3 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ tasks:
- task: db-up
- sleep 2
- atlas migrate diff {{.CLI_ARGS}} --dir "file://sql/migrations" --to "file://sql/schema.sql" --dev-url "postgresql://basemind:basemind@localhost:5432/basemind?search_path=public&sslmode=disable"
sqlc-generate-win:
cmds:
- docker run --rm -v ${PWD}:/src -w /src kjconroy/sqlc generate
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
start_period: 5s
dashboard-backend:
build:
dockerfile: Dockerfile
dockerfile: docker/Dockerfile
context: .
args:
BUILD_TARGET: dashboard-backend
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ WORKDIR /go/src/app
ARG BUILD_TARGET
COPY go.mod go.sum ./
RUN go mod download
COPY db db
#COPY db db
COPY go-shared lib
COPY go-services/$BUILD_TARGET services/$BUILD_TARGET
RUN CGO_ENABLED=0 go build -o /go/bin/app github.com/basemind-ai/monorepo/go-services/$BUILD_TARGET
Expand Down
64 changes: 55 additions & 9 deletions go-services/dashboard-backend/api/api.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
package api

import (
"net/http"

"github.com/basemind-ai/monorepo/go-services/dashboard-backend/config"
"github.com/basemind-ai/monorepo/go-services/dashboard-backend/constants"
"github.com/basemind-ai/monorepo/go-shared/apierror"
"github.com/basemind-ai/monorepo/go-shared/db"
"github.com/basemind-ai/monorepo/go-shared/serialization"
"net/http"

"github.com/go-chi/chi/v5"
)

func HandleDashboardUserPostLogin(w http.ResponseWriter, r *http.Request) {
firebaseId := r.Context().Value(constants.FireBaseIdContextKey).(string)

if userExists, queryErr := db.GetQueries().CheckUserExists(r.Context(), firebaseId); queryErr != nil {
// respond with internal server error
panic("not implemented")
_ = apierror.InternalServerError(queryErr.Error()).Render(w, r)
} else if !userExists {
// insert a new user here and create a default project for the user connected with an owner level permission
// response with the user (DB id, project and project permission)
panic("not implemented")
user, userQueryErr := db.GetQueries().CreateUser(r.Context(), firebaseId)
if userQueryErr != nil {
_ = apierror.InternalServerError(userQueryErr.Error()).Render(w, r)
return
}

project, projectQueryErr := db.GetQueries().CreateProject(r.Context(), db.CreateProjectParams{Name: "Default Project", Description: "Default Project"})
if projectQueryErr != nil {
_ = apierror.InternalServerError(projectQueryErr.Error()).Render(w, r)
return
}

userProject, userProjectQueryErr := db.GetQueries().CreateUserProject(r.Context(), db.CreateUserProjectParams{
UserID: user.ID,
ProjectID: project.ID,
Permission: db.AccessPermissionTypeADMIN,
IsUserDefaultProject: true,
})
if userProjectQueryErr != nil {
_ = apierror.InternalServerError(userProjectQueryErr.Error()).Render(w, r)
return
}

var userProjects = []db.FindProjectsByUserIdRow{{
ID: project.ID,
CreatedAt: project.CreatedAt,
Name: project.Name,
Description: project.Description,
Permission: userProject.Permission,
IsUserDefaultProject: userProject.IsUserDefaultProject,
}}

responseDTO := HandleDashboardUserPostLoginDTO{User: user, Projects: userProjects}

_ = serialization.RenderJsonResponse(w, http.StatusCreated, responseDTO)
} else {
// retrieve the user and project from the db and return them as a response with the permission
panic("not implemented")
user, queryErr := db.GetQueries().FindUserByFirebaseId(r.Context(), firebaseId)
if queryErr != nil {
_ = apierror.InternalServerError(queryErr.Error()).Render(w, r)
return
}

userProjects, projectsQueryErr := db.GetQueries().FindProjectsByUserId(r.Context(), user.ID)
if projectsQueryErr != nil {
_ = apierror.InternalServerError(projectsQueryErr.Error()).Render(w, r)
return
}

responseDTO := HandleDashboardUserPostLoginDTO{User: user, Projects: userProjects}

_ = serialization.RenderJsonResponse(w, http.StatusOK, responseDTO)
}
}

Expand Down
8 changes: 8 additions & 0 deletions go-services/dashboard-backend/api/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api

import "github.com/basemind-ai/monorepo/go-shared/db"

type HandleDashboardUserPostLoginDTO struct {
User db.User `json:"user"`
Projects []db.FindProjectsByUserIdRow `json:"projects"`
}
116 changes: 116 additions & 0 deletions go-shared/db/query.sql.go

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

1 change: 1 addition & 0 deletions go-shared/httpclient/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpclient_test
import (
"context"
"encoding/json"
"github.com/basemind-ai/monorepo/go-shared/httpclient/testutils"
"io"
"net/http"
"testing"
Expand Down
18 changes: 18 additions & 0 deletions sql/query.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
-- name: CheckUserExists :one
SELECT EXISTS(SELECT 1 FROM "user" WHERE firebase_id = $1);

-- name: CreateUser :one
INSERT INTO "user" (firebase_id) values ($1)
RETURNING *;

-- name: FindUserByFirebaseId :one
SELECT id, firebase_id, created_at from "user" where firebase_id = $1;

-- name: FindProjectsByUserId :many
Select id, name, description, permission, is_user_default_project, created_at from project p inner join user_project up on p.id = up.project_id where up.user_id = $1;

-- name: CreateProject :one
INSERT INTO project (name, description) values ($1, $2)
RETURNING *;

-- name: CreateUserProject :one
INSERT INTO user_project (user_id, project_id, permission, is_user_default_project) values ($1, $2, $3, $4)
RETURNING *;

0 comments on commit 1621729

Please sign in to comment.