Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api stablizing #6

Merged
merged 14 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions api/swagger/docs.go

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

3 changes: 0 additions & 3 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1427,9 +1427,6 @@
"accountId": {
"type": "string"
},
"authorized": {
"type": "boolean"
},
"createdAt": {
"type": "string"
},
Expand Down
2 changes: 0 additions & 2 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,6 @@ definitions:
properties:
accountId:
type: string
authorized:
type: boolean
createdAt:
type: string
creator:
Expand Down
13 changes: 4 additions & 9 deletions internal/delivery/http/auth.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package http

import (
"encoding/json"
"io"
"github.com/openinfradev/tks-api/pkg/log"
"net/http"

"github.com/openinfradev/tks-api/internal/usecase"
Expand Down Expand Up @@ -39,20 +38,16 @@ func NewAuthHandler(h usecase.IAuthUsecase) IAuthHandler {
// @Router /auth/login [post]
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
input := domain.LoginRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

err = json.Unmarshal(body, &input)
err := UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

user, err := h.usecase.Login(input.AccountId, input.Password, input.OrganizationId)
if err != nil {
log.Error("error is :%s(%T)", err.Error(), err)

ErrorJSON(w, err)
return
}
Expand Down
80 changes: 27 additions & 53 deletions internal/delivery/http/organization.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package http

import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/openinfradev/tks-api/pkg/httpErrors"
"net/http"

"github.com/gorilla/mux"
"github.com/openinfradev/tks-api/internal/auth/request"
Expand Down Expand Up @@ -39,56 +35,30 @@ func NewOrganizationHandler(o usecase.IOrganizationUsecase, u usecase.IUserUseca
// @Router /organizations [post]
// @Security JWT
func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.Request) {

input := domain.CreateOrganizationRequest{}

err := UnmarshalRequestInput(r, &input)
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

/*
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

err = json.Unmarshal(body, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

err = validate.Struct(input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

*/
ctx := r.Context()
organization := input.ToOrganization()
organization.Creator = ""

token, ok := request.TokenFrom(r.Context())
if !ok {
ErrorJSON(w, httpErrors.NewUnauthorizedError(fmt.Errorf("token not found")))
return
}
organizationId, err := h.usecase.Create(domain.Organization{
Name: input.Name,
Creator: "",
Description: input.Description,
}, token)
organizationId, err := h.usecase.Create(ctx, organization)
if err != nil {
log.Error("Failed to create organization err : ", err)
log.Errorf("error is :%s(%T)", err.Error(), err)
ErrorJSON(w, err)
return
}

// Admin user 생성
_, err = h.userUsecase.CreateAdmin(organizationId)
if err != nil {
log.Error("Failed to create user err : ", err)
log.Errorf("error is :%s(%T)", err.Error(), err)
ErrorJSON(w, err)
return
}
Expand All @@ -99,9 +69,8 @@ func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.

out.OrganizationId = organizationId

time.Sleep(time.Second * 5) // for test
//time.Sleep(time.Second * 5) // for test
ResponseJSON(w, http.StatusOK, out)

}

// GetOrganizations godoc
Expand All @@ -117,6 +86,8 @@ func (h *OrganizationHandler) GetOrganizations(w http.ResponseWriter, r *http.Re
log.Info("GetOrganization")
organizations, err := h.usecase.Fetch()
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)

ErrorJSON(w, err)
return
}
Expand All @@ -125,7 +96,9 @@ func (h *OrganizationHandler) GetOrganizations(w http.ResponseWriter, r *http.Re
Organizations []domain.Organization `json:"organizations"`
}

out.Organizations = organizations
for _, organization := range *organizations {
out.Organizations = append(out.Organizations, organization)
}

ResponseJSON(w, http.StatusOK, out)
}
Expand All @@ -150,6 +123,8 @@ func (h *OrganizationHandler) GetOrganization(w http.ResponseWriter, r *http.Req

organization, err := h.usecase.Get(organizationId)
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)

ErrorJSON(w, err)
return
}
Expand Down Expand Up @@ -187,16 +162,19 @@ func (h *OrganizationHandler) DeleteOrganization(w http.ResponseWriter, r *http.
return
}

// TODO : organization에 속한 user들도 삭제해야함(DB에는 남아있음)
// Admin user 삭제
err := h.userUsecase.DeleteAdmin(organizationId)
ResponseJSON(w, http.StatusOK, nil)
err := h.userUsecase.DeleteAll(r.Context(), organizationId)
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)

// TODO: user 삭제
ErrorJSON(w, err)
return
}

// organization 삭제
err = h.usecase.Delete(organizationId, token)
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)

ErrorJSON(w, err)
return
}
Expand All @@ -223,20 +201,16 @@ func (h *OrganizationHandler) UpdateOrganization(w http.ResponseWriter, r *http.
}

input := domain.UpdateOrganizationRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Error(err)
return
}

err = json.Unmarshal(body, &input)
err := UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

err = h.usecase.Update(organizationId, input)
if err != nil {
log.Errorf("error is :%s(%T)", err.Error(), err)

ErrorJSON(w, err)
return
}
Expand Down
Loading