-
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 #46 from mattrltrent/watched_schools
Adds watched schools
- Loading branch information
Showing
10 changed files
with
243 additions
and
9 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
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,49 @@ | ||
package schools | ||
|
||
import ( | ||
"confesi/lib/response" | ||
"confesi/lib/utils" | ||
"net/http" | ||
|
||
"firebase.google.com/go/auth" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// value that gets sent back to client for each of their watched schools | ||
type schoolResult struct { | ||
ID uint `json:"id"` | ||
Name string `json:"name"` | ||
Abbr string `json:"abbr"` | ||
Lat string `json:"lat"` | ||
Lon string `json:"lon"` | ||
Domain string `json:"domain"` | ||
} | ||
|
||
func (h *handler) getWatchedSchools(c *gin.Context, token *auth.Token) ([]schoolResult, error) { | ||
schools := []schoolResult{} | ||
err := h.DB. | ||
Table("school_follows"). | ||
Select("schools.id as id, schools.name, schools.abbr, schools.lat, schools.lon, schools.domain"). | ||
Joins("JOIN schools ON school_follows.school_id = schools.id"). | ||
Find(&schools).Error | ||
if err != nil { | ||
return nil, serverError | ||
} | ||
return schools, nil | ||
} | ||
|
||
// TODO: should this be limited to only N schools? Paginated? Or | ||
// TODO: will this be cached locally so we'd want to get everything? | ||
func (h *handler) handleGetWatchedSchools(c *gin.Context) { | ||
token, err := utils.UserTokenFromContext(c) | ||
if err != nil { | ||
response.New(http.StatusInternalServerError).Err(serverError.Error()).Send(c) | ||
return | ||
} | ||
schools, err := h.getWatchedSchools(c, token) | ||
if err != nil { | ||
response.New(http.StatusInternalServerError).Err(err.Error()).Send(c) | ||
return | ||
} | ||
response.New(http.StatusOK).Val(schools).Send(c) | ||
} |
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,45 @@ | ||
package schools | ||
|
||
import ( | ||
"confesi/db" | ||
"confesi/lib/response" | ||
"confesi/lib/utils" | ||
"confesi/lib/validation" | ||
"net/http" | ||
|
||
"firebase.google.com/go/auth" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func (h *handler) unwatchSchool(c *gin.Context, token *auth.Token, req validation.WatchSchool) error { | ||
school := db.SchoolFollow{ | ||
UserID: token.UID, | ||
SchoolID: req.SchoolID, | ||
} | ||
err := h.DB.Delete(&school, "user_id = ? AND school_id = ?", school.UserID, school.SchoolID).Error | ||
if err != nil { | ||
return serverError | ||
} | ||
return nil | ||
} | ||
|
||
func (h *handler) handleUnwatchSchool(c *gin.Context) { | ||
// extract request | ||
var req validation.WatchSchool | ||
|
||
err := utils.New(c).Validate(&req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
token, err := utils.UserTokenFromContext(c) | ||
if err != nil { | ||
response.New(http.StatusInternalServerError).Err(serverError.Error()).Send(c) | ||
return | ||
} | ||
err = h.unwatchSchool(c, token, req) | ||
if err != nil { | ||
response.New(http.StatusInternalServerError).Err(err.Error()).Send(c) | ||
} | ||
response.New(http.StatusOK).Send(c) | ||
} |
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,68 @@ | ||
package schools | ||
|
||
import ( | ||
"confesi/db" | ||
"confesi/lib/response" | ||
"confesi/lib/utils" | ||
"confesi/lib/validation" | ||
"errors" | ||
"net/http" | ||
|
||
"firebase.google.com/go/auth" | ||
"github.com/gin-gonic/gin" | ||
"github.com/jackc/pgx/v5/pgconn" | ||
) | ||
|
||
func (h *handler) watchSchool(c *gin.Context, token *auth.Token, req validation.WatchSchool) error { | ||
school := db.SchoolFollow{ | ||
UserID: token.UID, | ||
SchoolID: req.SchoolID, | ||
} | ||
err := h.DB.Create(&school).Error | ||
if err != nil { | ||
var pgErr *pgconn.PgError | ||
// Gorm doesn't properly handle duplicate errors: https://github.com/go-gorm/gorm/issues/4037 | ||
if ok := errors.As(err, &pgErr); !ok { | ||
// if it's not a PostgreSQL error, return a generic server error | ||
return serverError | ||
} | ||
switch pgErr.Code { | ||
case "23505": // duplicate key value violates unique constraint | ||
return nil // just let the user know it's been watched, if it's already there | ||
case "23503": // foreign key constraint violation | ||
return invalidId // aka, you provided an school id to try watching | ||
default: | ||
// some other postgreSQL error | ||
return serverError | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (h *handler) handleWatchSchool(c *gin.Context) { | ||
// extract request | ||
var req validation.WatchSchool | ||
|
||
err := utils.New(c).Validate(&req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
token, err := utils.UserTokenFromContext(c) | ||
if err != nil { | ||
response.New(http.StatusInternalServerError).Err(serverError.Error()).Send(c) | ||
return | ||
} | ||
err = h.watchSchool(c, token, req) | ||
if err != nil { | ||
// switch over err | ||
switch err { | ||
case invalidId: | ||
response.New(http.StatusBadRequest).Err(err.Error()).Send(c) | ||
default: | ||
response.New(http.StatusInternalServerError).Err(err.Error()).Send(c) | ||
} | ||
return | ||
} | ||
response.New(http.StatusCreated).Send(c) | ||
} |
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,7 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE school_follows | ||
DROP COLUMN created_at, | ||
DROP COLUMN updated_at; | ||
|
||
END; |
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,7 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE school_follows | ||
ADD COLUMN created_at TIMESTAMPTZ NOT NULL, | ||
ADD COLUMN updated_at TIMESTAMPTZ; | ||
|
||
END; |