Backend/internal/api/services/users/users_getbyid.go
zervo a3e044dec5 Improved strings, CI & schedules base
Improved some strings, added the basics to prepare for integrating schedules again, and added basic Forgejo CI.
2024-12-06 21:53:37 +01:00

68 lines
2 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 users
import (
"github.com/kataras/iris/v12"
db "git.zervo.org/scheduletogether/backend/internal/database"
"git.zervo.org/scheduletogether/backend/pkg/types"
)
// UsersGetById_Service gets a single user from the database by id
func UsersGetById_Service(ctx iris.Context) {
// Get user ID from URL parameters
id, err := ctx.Params().GetUint64("id")
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(types.CommonErrorResponse{
Error: "Invalid id parameter",
})
return
}
// Get user from database
var user db.User
err = db.Db.Model(&db.User{}).Where("id = ?", id).Select("id", "username", "email", "uuid", "role", "first_name", "last_name", "verified", "friends").Find(&user).Error
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.JSON(types.CommonErrorResponse{
Error: "Failed to retrieve user",
})
logboi.Warn("Failed to retrieve user from the database: " + err.Error())
return
}
if user.ID == 0 {
ctx.StatusCode(iris.StatusNotFound)
ctx.JSON(types.CommonErrorResponse{
Error: "User not found",
})
return
}
// Return user
ctx.JSON(user)
}