751c841fa2
- Configured VSCode tasks. - Fixed some grammar mistakes. - Added OpenAPI documentation.
97 lines
2.9 KiB
Go
Executable file
97 lines
2.9 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 friends
|
|
|
|
import (
|
|
"github.com/kataras/iris/v12"
|
|
|
|
db "git.zervo.org/scheduletogether/backend/internal/database"
|
|
"git.zervo.org/scheduletogether/backend/pkg/types"
|
|
)
|
|
|
|
func FriendsGetRequests_Service(ctx iris.Context, id uint) {
|
|
// Make sure a userId was passed
|
|
if id == 0 {
|
|
ctx.StatusCode(iris.StatusInternalServerError)
|
|
ctx.JSON(types.CommonErrorResponse{
|
|
Error: "Something went wrong, please try again later",
|
|
})
|
|
logboi.Warn("Got userId 0 in FriendsGetRequests_Service, which is not expected behaviour")
|
|
return
|
|
}
|
|
|
|
// Get target user's information
|
|
var user db.User
|
|
err := db.Db.Where("id = ?", id).First(&user).Error
|
|
if err != nil {
|
|
ctx.StatusCode(iris.StatusInternalServerError)
|
|
ctx.JSON(types.CommonErrorResponse{
|
|
Error: "Something went wrong, please try again later",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Get friend requests targeted at user
|
|
var friendRequests []db.FriendRequest
|
|
err = db.Db.Where("target = ?", user.Uuid).Find(&friendRequests).Error
|
|
if err == nil { // CHECK
|
|
ctx.StatusCode(iris.StatusBadRequest)
|
|
ctx.JSON(types.CommonErrorResponse{
|
|
Error: "Something went wrong, please try again later",
|
|
})
|
|
return
|
|
}
|
|
|
|
length := len(friendRequests)
|
|
var response []types.FriendRequest
|
|
|
|
// Loop over friend requests to rewrite to response format
|
|
for _, request := range friendRequests {
|
|
// Get source user information
|
|
var sourceUser db.User
|
|
err = db.Db.Where("uuid = ?", request.Source).First(&sourceUser).Error
|
|
if err != nil {
|
|
ctx.StatusCode(iris.StatusInternalServerError)
|
|
ctx.JSON(types.CommonErrorResponse{
|
|
Error: "Something went wrong, please try again later",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Add to response slice
|
|
response = append(response, types.FriendRequest{
|
|
CreatedAt: request.CreatedAt.String(),
|
|
SourceUsername: sourceUser.Username,
|
|
SourceUuid: request.Source,
|
|
TargetUuid: request.Target,
|
|
})
|
|
}
|
|
|
|
// Write response
|
|
ctx.JSON(types.FriendsGetRequestsResponse{
|
|
Amount: length,
|
|
Requests: response,
|
|
})
|
|
}
|