Backend/internal/api/handlers/friends_handler.go
2024-12-06 21:53:09 +01:00

103 lines
3.8 KiB
Go
Executable file

/*
ScheduleTogether Backend
Copyright (C) 2024, Zervó Zadachin
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License version 3 for more details.
This program incorporates external libraries for certain functionalities.
These libraries are covered by their respective licenses, and their usage
agreements are as outlined in their respective documentation or source
code.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package handlers
import (
"github.com/kataras/iris/v12"
"git.zervo.org/scheduletogether/backend/internal/api/middlewares"
"git.zervo.org/scheduletogether/backend/internal/api/services/friends"
"git.zervo.org/scheduletogether/backend/pkg/helpers/request"
perms "git.zervo.org/scheduletogether/backend/pkg/permissions"
"git.zervo.org/scheduletogether/backend/pkg/types"
)
// FriendHandler_RegisterRoutes registers routes for the AccountHandler
func FriendsHandler_RegisterRoutes(party iris.Party) {
party.Use(middlewares.Authenticate()) // only allow authenticated users for following endpoints
party.Post("/add", middlewares.Authorize(perms.SendFriendRequest), FriendAdd_Handler)
party.Post("/accept", middlewares.Authorize(perms.AcceptFriendRequest), FriendAccept_Handler)
party.Get("/", middlewares.Authorize(perms.GetOwnFriendRequests), FriendsGetRequests_Handler)
}
// FriendAdd_Handler handles the POST /friends/add endpoint
func FriendAdd_Handler(ctx iris.Context) {
var requestBody types.FriendsAddRequest
// Validate the request body against the FriendsAddRequest schema
if err := ctx.ReadJSON(&requestBody); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(types.CommonErrorResponse{
Error: "Request validation failed, please check the request body and try again",
})
return
}
// Call the FriendsAdd_Service function to add a friend request to another user
friends.FriendAdd_Service(ctx, requestBody)
}
// FriendAccept_Handler handles the POST /friends/accept endpoint
func FriendAccept_Handler(ctx iris.Context) {
var requestBody types.FriendsAcceptRequest
// Validate the request body against the FriendsAcceptRequest schema
if err := ctx.ReadJSON(&requestBody); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(types.CommonErrorResponse{
Error: "Request validation failed, please check the request body and try again",
})
return
}
// Call the FriendsAccept_Service function to accept a friend request
friends.FriendAccept_Service(ctx, requestBody)
}
// FriendsGetRequests_Handler handles the GET /friends endpoint
func FriendsGetRequests_Handler(ctx iris.Context) {
// Get sending user ID
claims := request.GetUserClaims(ctx)
id := claims.UserId
// Call the FriendsGetRequests_Service function to get pending friend requests for user
friends.FriendsGetRequests_Service(ctx, id)
}
// FriendsGetRequests_Handler handles the GET /friends/:id endpoint
func FriendsGetRequestsById_Handler(ctx iris.Context) {
// Get target user ID from URL
id, err := ctx.Params().GetUint64("id")
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(types.CommonErrorResponse{
Error: "Request validation failed, please check the request parameters and try again",
})
return
}
// Call the FriendsGetRequests_Service function to get pending friend requests for user
friends.FriendsGetRequests_Service(ctx, uint(id))
}