list channels

This commit is contained in:
BENEDEK László 2025-06-05 14:34:46 +02:00
parent ecd996e21e
commit d5ae19a0a4
5 changed files with 66 additions and 3 deletions

22
api/chat.go Normal file
View File

@ -0,0 +1,22 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
func listAvailableChannels(c *gin.Context) {
token, _ := c.Get(SESSION_COOKIE)
channels, err := chatController.ListAvailableChannels(token.(string))
if err != nil {
sendError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"message": "channels listed",
"channels": channels,
})
}

View File

@ -32,7 +32,7 @@ func initControlles() *util.ChatError {
userController = controller.MakeUserController(userDAO) userController = controller.MakeUserController(userDAO)
authController = controller.MakeAuthController(userDAO, sessionDAO) authController = controller.MakeAuthController(userDAO, sessionDAO)
chatController = controller.MakeChatController(channelDAO) chatController = controller.MakeChatController(channelDAO, sessionDAO)
return nil return nil
} }
@ -56,6 +56,10 @@ func Listen(address string, base string) error {
user.Use(isLoggedIn) user.Use(isLoggedIn)
user.GET("info/:username", userInfo) user.GET("info/:username", userInfo)
chat := api.Group("chat")
chat.Use(isLoggedIn)
chat.GET("channels", listAvailableChannels)
server := &http.Server{ server := &http.Server{
Addr: address, Addr: address,
Handler: router, Handler: router,

View File

@ -2,13 +2,25 @@ package controller
import ( import (
"git.tek.govt.hu/dowerx/chat/server/dao" "git.tek.govt.hu/dowerx/chat/server/dao"
"git.tek.govt.hu/dowerx/chat/server/model"
"git.tek.govt.hu/dowerx/chat/server/util"
) )
type ChatController struct { type ChatController struct {
channelDAO dao.IChannelDAO channelDAO dao.IChannelDAO
sessionDAO dao.ISessionDAO
} }
func MakeChatController(channelDAO dao.IChannelDAO) ChatController { func (c ChatController) ListAvailableChannels(token string) ([]model.Channel, *util.ChatError) {
controller := ChatController{channelDAO: channelDAO} userID, err := c.sessionDAO.Get(token)
if err != nil {
return nil, err
}
return c.channelDAO.ListAvailableChannels(userID)
}
func MakeChatController(channelDAO dao.IChannelDAO, sessionDAO dao.ISessionDAO) ChatController {
controller := ChatController{channelDAO: channelDAO, sessionDAO: sessionDAO}
return controller return controller
} }

View File

@ -9,6 +9,7 @@ type IChannelDAO interface {
Create(channel model.Channel) *util.ChatError Create(channel model.Channel) *util.ChatError
Read(channel model.Channel) (model.Channel, *util.ChatError) Read(channel model.Channel) (model.Channel, *util.ChatError)
List() ([]model.Channel, *util.ChatError) List() ([]model.Channel, *util.ChatError)
ListAvailableChannels(userID int) ([]model.Channel, *util.ChatError)
Update(channel model.Channel) *util.ChatError Update(channel model.Channel) *util.ChatError
Delete(channel model.Channel) *util.ChatError Delete(channel model.Channel) *util.ChatError
} }

View File

@ -58,6 +58,30 @@ func (d ChannelDAOPG) List() ([]model.Channel, *util.ChatError) {
return channels, util.MakeError(err, util.DATABASE_QUERY_FAULT) return channels, util.MakeError(err, util.DATABASE_QUERY_FAULT)
} }
// ListAvailableChannels returns channels that the given user has access to
func (d ChannelDAOPG) ListAvailableChannels(userID int) ([]model.Channel, *util.ChatError) {
rows, err := d.db.Queryx(`select "channel_id" as "id", "channel_name" as "name", "channel_description" as "description" from "user_rigths_per_channel" where "user_id" = $1`, userID)
if err != nil {
return nil, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
defer rows.Close()
channels := make([]model.Channel, 0)
for rows.Next() {
channel := model.Channel{}
err = rows.StructScan(&channel)
if err != nil {
break
}
channels = append(channels, channel)
}
return channels, util.MakeError(err, util.DATABASE_QUERY_FAULT)
}
// Update sets all the fields of the Channel with the given ID // Update sets all the fields of the Channel with the given ID
func (d ChannelDAOPG) Update(channel model.Channel) *util.ChatError { func (d ChannelDAOPG) Update(channel model.Channel) *util.ChatError {
_, err := d.db.NamedExec(`update "channel" set "name" = :name, "description" = :description where "id" = :id`, &channel) _, err := d.db.NamedExec(`update "channel" set "name" = :name, "description" = :description where "id" = :id`, &channel)