generated from akmamun/gin-boilerplate
-
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.
Merge pull request #169 from osvauld/develop
Develop
- Loading branch information
Showing
59 changed files
with
2,176 additions
and
257 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,167 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"osvauld/customerrors" | ||
dto "osvauld/dtos" | ||
"osvauld/service" | ||
"osvauld/utils" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func AddEnvironment(ctx *gin.Context) { | ||
caller, err := utils.FetchUserIDFromCtx(ctx) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid user id")) | ||
return | ||
} | ||
var req dto.AddEnvironment | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", err) | ||
return | ||
} | ||
_, err = service.AddEnvironment(ctx, req, caller) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusOK, nil, "added environment", nil) | ||
} | ||
|
||
func GetEnvironments(ctx *gin.Context) { | ||
caller, err := utils.FetchUserIDFromCtx(ctx) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid user id")) | ||
return | ||
} | ||
environments, err := service.GetEnvironments(ctx, caller) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusOK, environments, "fetched environments", nil) | ||
} | ||
|
||
func GetEnvironmentFields(ctx *gin.Context) { | ||
// caller, err := utils.FetchUserIDFromCtx(ctx) | ||
// if err != nil { | ||
// SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid user id")) | ||
// return | ||
// } | ||
environmentIDStr := ctx.Param("id") | ||
environmentID, err := uuid.Parse(environmentIDStr) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid environment id")) | ||
return | ||
} | ||
// TODO: Add check for user access to environment | ||
credentials, err := service.GetEnvironmentFields(ctx, environmentID) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusOK, credentials, "fetched credentials", nil) | ||
} | ||
|
||
func GetEnvironmentByName(ctx *gin.Context) { | ||
caller, err := utils.FetchUserIDFromCtx(ctx) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid user id")) | ||
return | ||
} | ||
environmentName := ctx.Param("name") | ||
environment, err := service.GetEnvironmentByName(ctx, environmentName, caller) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusInternalServerError, nil, "", errors.New("failed to fetch environment")) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusOK, environment, "Fetched environment", nil) | ||
} | ||
|
||
func EditEnvironmentFieldName(ctx *gin.Context) { | ||
caller, err := utils.FetchUserIDFromCtx(ctx) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid user id")) | ||
return | ||
} | ||
|
||
var req dto.EditEnvFieldName | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", err) | ||
return | ||
} | ||
|
||
response, err := service.EditEnvFieldName(ctx, req, caller) | ||
if err != nil { | ||
if _, ok := err.(*customerrors.UserDoesNotHaveEnvironmentAccess); ok { | ||
SendResponse(ctx, http.StatusUnauthorized, nil, "", err) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
SendResponse(ctx, http.StatusOK, response, "", nil) | ||
|
||
} | ||
|
||
func GetCredentialEnvFieldsForEditDataSync(ctx *gin.Context) { | ||
userID, err := utils.FetchUserIDFromCtx(ctx) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusUnauthorized, nil, "", errors.New("unauthorized")) | ||
return | ||
} | ||
|
||
credentialIDStr := ctx.Param("credentialId") | ||
credentailID, err := uuid.Parse(credentialIDStr) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid credential id")) | ||
return | ||
} | ||
|
||
fieldData, err := service.GetCredentialEnvFieldsForEditDataSync(ctx, credentailID, userID) | ||
if err != nil { | ||
|
||
if _, ok := err.(*customerrors.UserDoesNotHaveCredentialAccessError); ok { | ||
SendResponse(ctx, http.StatusUnauthorized, nil, "", err) | ||
return | ||
} | ||
|
||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
|
||
SendResponse(ctx, http.StatusOK, fieldData, "Fetched Fields", nil) | ||
|
||
} | ||
|
||
func GetEnvsForCredential(ctx *gin.Context) { | ||
// userID, err := utils.FetchUserIDFromCtx(ctx) | ||
// if err != nil { | ||
// SendResponse(ctx, http.StatusUnauthorized, nil, "", errors.New("unauthorized")) | ||
// return | ||
// } | ||
|
||
credentialIDStr := ctx.Param("credentialId") | ||
credentailID, err := uuid.Parse(credentialIDStr) | ||
if err != nil { | ||
SendResponse(ctx, http.StatusBadRequest, nil, "", errors.New("invalid credential id")) | ||
return | ||
} | ||
|
||
envs, err := service.GetEnvsForCredential(ctx, credentailID) | ||
if err != nil { | ||
|
||
if _, ok := err.(*customerrors.UserDoesNotHaveCredentialAccessError); ok { | ||
SendResponse(ctx, http.StatusUnauthorized, nil, "", err) | ||
return | ||
} | ||
|
||
SendResponse(ctx, http.StatusInternalServerError, nil, "", err) | ||
return | ||
} | ||
|
||
SendResponse(ctx, http.StatusOK, envs, "Fetched Environments", 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
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 @@ | ||
DROP TABLE IF EXISTS field_values; | ||
|
||
DROP TABLE IF EXISTS field_data; | ||
|
||
ALTER TABLE folders DROP COLUMN IF EXISTS type ; |
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,19 @@ | ||
CREATE TABLE field_data ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
field_name VARCHAR(255) NOT NULL, | ||
field_type VARCHAR(255) NOT NULL, | ||
credential_id UUID NOT NULL REFERENCES credentials(id) ON DELETE CASCADE, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
created_by UUID REFERENCES users(id) ON DELETE SET NULL, | ||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
updated_by UUID REFERENCES users(id) ON DELETE SET NULL | ||
); | ||
|
||
CREATE TABLE field_values ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
field_id UUID NOT NULL REFERENCES field_data(id) ON DELETE CASCADE, | ||
field_value TEXT NOT NULL, | ||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE | ||
); | ||
|
||
ALTER TABLE folders ADD COLUMN type VARCHAR(255) NOT NULL DEFAULT 'shared'; |
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 @@ | ||
DROP TABLE IF EXISTS environment_fields; | ||
|
||
DROP TABLE IF EXISTS environments; | ||
|
||
ALTER TABLE users DROP COLUMN IF EXISTS created_by; |
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,22 @@ | ||
|
||
ALTER TABLE users ADD COLUMN created_by UUID; | ||
|
||
CREATE TABLE environments ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
cli_user UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, | ||
name VARCHAR(255) NOT NULL, | ||
createdAt TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
updatedAt TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
created_by UUID NOT NULL REFERENCES users(id) | ||
); | ||
|
||
CREATE TABLE environment_fields ( | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
field_name VARCHAR(255) NOT NULL, | ||
field_value TEXT NOT NULL, | ||
parent_field_value_id UUID NOT NULL REFERENCES field_values(id) ON DELETE CASCADE, | ||
env_id UUID NOT NULL REFERENCES environments(Id) ON DELETE CASCADE, | ||
credential_id UUID NOT NULL REFERENCES credentials(id) ON DELETE CASCADE, | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
); |
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
Oops, something went wrong.