Skip to content

Commit

Permalink
fix: status code related to group (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Nov 26, 2022
1 parent 8e778c4 commit 3f3b43c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
16 changes: 12 additions & 4 deletions api/blogs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
)
Expand All @@ -24,12 +26,18 @@ func (server *Server) getAllBlogs(w http.ResponseWriter, r *http.Request) {
// get group name from query parameters
group := r.FormValue("gn")

if !server.querier.ExistGroup(group) {
// return error message
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Error: No valid group name"))
_, err := server.querier.FindGroupByName(group)
if err != nil {
if errors.Unwrap(err) == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name was passed."))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading group from DB"))
return
}

blogs, err := server.querier.GetAllBlogs(group)
if err != nil {
// db error
Expand Down
16 changes: 11 additions & 5 deletions api/formations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"

Expand All @@ -24,11 +26,15 @@ func (server *Server) getAllFormations(w http.ResponseWriter, r *http.Request) {
// get group name from query parameters
group := r.FormValue("gn")

if !server.querier.ExistGroup(group) {
fmt.Printf("getAllFormations: access to invalid group name")
// return error message
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name"))
_, err := server.querier.FindGroupByName(group)
if err != nil {
if errors.Unwrap(err) == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name was passed."))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading group from DB"))
return
}

Expand Down
19 changes: 13 additions & 6 deletions api/members.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"

Expand All @@ -25,24 +27,29 @@ func (server *Server) getAllMembers(w http.ResponseWriter, r *http.Request) {

if !server.isApiKeyValid(key) {
// return error message
w.WriteHeader(http.StatusForbidden)
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, ErrorJson("No valid api key"))
return
}

// get group name from query parameters
group := r.FormValue("gn")

if !server.querier.ExistGroup(group) {
// return error message
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Error while"))
_, err := server.querier.FindGroupByName(group)
if err != nil {
if errors.Unwrap(err) == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name was passed."))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading group from DB"))
return
}

// 言語情報を取得する。
var l *models.Locale
l, err := server.querier.FindLocaleByName(locale)
l, err = server.querier.FindLocaleByName(locale)
if err != nil {
// DB に見つからなかった場合、デフォルトの情報を取得。
l, _ = server.querier.FindLocaleByName("ja")
Expand Down
17 changes: 11 additions & 6 deletions api/songs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
)
Expand All @@ -23,14 +25,17 @@ func (server *Server) getAllSongs(w http.ResponseWriter, r *http.Request) {
// get group name from query parameters
group := r.FormValue("gn")

if !server.querier.ExistGroup(group) {
fmt.Printf("getAllSongs: access to invalid group name")
// return error message
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name"))
_, err := server.querier.FindGroupByName(group)
if err != nil {
if errors.Unwrap(err) == sql.ErrNoRows {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, ErrorJson("Invalid group name was passed."))
return
}
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, ErrorJson("Error while reading group from DB"))
return
}

dr, err := server.querier.GetAllSongs(group)
if err != nil {
fmt.Printf("getAllSongs: %v", err)
Expand Down
7 changes: 0 additions & 7 deletions db/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
models "github.com/android-project-46group/api-server/db/my_models"
)

func (q *SqlQuerier) ExistGroup(groupName string) bool {

_, err := models.Groups(qm.Where("group_name = ?", groupName)).One(q.ctx, q.DB)

return err == nil
}

func (q *SqlQuerier) FindGroupByName(groupName string) (*models.Group, error) {

g, err := models.Groups(qm.Where("group_name = ?", groupName)).One(q.ctx, q.DB)
Expand Down
1 change: 0 additions & 1 deletion db/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type Querier interface {
GetAllFormations(groupName string) ([]PositionSongsBind, error)
GetFormations(groupName string) ([]PositionSongsBind, error)
FindGroupByName(groupName string) (*models.Group, error)
ExistGroup(groupName string) bool
FindLocaleByName(name string) (*models.Locale, error)
GetAllMemberInfos(groupName string, localeId int) ([]MemberInfoBind, error)
GetAllPositions(groupName string) ([]MemberInfoBind, error)
Expand Down

0 comments on commit 3f3b43c

Please sign in to comment.