Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
PATCH: pagination handling and enable nullable subscription IDs
Browse files Browse the repository at this point in the history
The commit improved the pagination utility to handle situations where the offset exceeds the number of records. It also updated the subscription handler to support nullable follower and following IDs. Both changes improve data consistency and cater to potential edge cases.
  • Loading branch information
930C committed Jan 27, 2024
1 parent faa1f63 commit b4d14fd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
4 changes: 0 additions & 4 deletions deployments/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ services:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}
ports:
- "${DB_PORT}:5432"
volumes:
- db_data:/var/lib/postgresql/data
- ./server_alpha_db.sql:/docker-entrypoint-initdb.d/initdb.sql
Expand All @@ -36,8 +34,6 @@ services:
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- MAILGUN_API_KEY=${MAILGUN_API_KEY}
ports:
- "${APP_PORT}:8080"
volumes:
- ${PWD}/private_key.pem:/private_key.pem
- ${PWD}/public_key.pem:/public_key.pem
Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"server-alpha/internal/schemas"
"server-alpha/internal/utils"
"strconv"
"strings"
"time"

"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -323,6 +324,12 @@ func determineFeedType(r *http.Request, w http.ResponseWriter, handler *PostHand
if authHeader == "" {
publicFeedWanted = true
} else {
if !strings.HasPrefix(authHeader, "Bearer ") || len(authHeader) <= len("Bearer ") {
err = errors.New("invalid authorization header")
utils.WriteAndLogError(w, schemas.InvalidToken, http.StatusBadRequest, err)
return false, nil, err
}

jwtToken := authHeader[len("Bearer "):]
claims, err = handler.JWTManager.ValidateJWT(jwtToken)
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions internal/handlers/subscription_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,12 @@ func (handler *SubscriptionHandler) HandleGetSubscriptions(w http.ResponseWriter
}

if followingId != uuid.Nil {
*subscription.FollowingId = followingId.String()
followingIdStr := followingId.String()
subscription.FollowingId = &followingIdStr
} else {
subscription.FollowingId = nil
}

if followerId != uuid.Nil {
*subscription.FollowerId = followerId.String()
}

results = append(results, subscription)
}

Expand Down
12 changes: 4 additions & 8 deletions internal/utils/pagination_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,17 @@ func ParsePaginationParams(r *http.Request) (int, int, error) {

func SendPaginatedResponse(w http.ResponseWriter, records interface{}, offset, limit, totalRecords int) {
// Get a reflect.Value of records.

v := reflect.ValueOf(records)
if offset > v.Len() {
offset = v.Len()
}

end := offset + limit
if end > v.Len() {
end = v.Len()
}

if v.Len() == 0 {
offset = 0
}

if offset > v.Len() {
offset = v.Len() - 1
}

// Check if v is not a slice.
if v.Kind() != reflect.Slice {
WriteAndLogError(w, schemas.BadRequest, http.StatusBadRequest, errors.New("records not a valid list"))
Expand Down

0 comments on commit b4d14fd

Please sign in to comment.