2024-12-04 19:46:11 -04:00

224 lines
6.8 KiB
Go

package grpc
import (
"context"
"git.maximotejeda.com/maximo/tgb-user/proto/golang/tgbuser"
"git.maximotejeda.com/maximo/tgb-user/internal/application/core/domain"
)
// TODO: Methods to map to api <-> grpc
func (a Adapter) Create(ctx context.Context, req *tgbuser.CreateTGBUserRequest) (*tgbuser.CreateTGBUserResponse, error) {
// TODO db work
user := &domain.User{
ID: req.User.Id,
TGB_ID: req.User.TgbId,
Username: req.User.Username,
FirstName: req.User.FirstName,
LastName: req.User.LastName,
TGBOT: req.User.TgBot,
}
_, err := a.api.Create(ctx, user)
if err != nil {
return nil, err
}
return &tgbuser.CreateTGBUserResponse{Id: req.User.Id}, nil
}
func (a Adapter) Get(ctx context.Context, req *tgbuser.GetTGBUserRequest) (*tgbuser.GetTGBUserResponse, error) {
// TODO db work
user, err := a.api.Get(ctx, req.TgbId)
if err != nil {
return nil, err
}
us := &tgbuser.User{
Id: user.ID,
TgbId: user.TGB_ID,
Username: user.Username,
FirstName: user.FirstName,
LastName: user.LastName,
TgBot: user.TGBOT,
Created: user.Created,
Edited: user.Edited,
}
return &tgbuser.GetTGBUserResponse{User: us}, err
}
func (a Adapter) Edit(ctx context.Context, req *tgbuser.EditTGBUserRequest) (*tgbuser.EditTGBUserResponse, error) {
// TODO db work
user := &domain.User{
ID: req.User.Id,
TGB_ID: req.User.TgbId,
Username: req.User.Username,
FirstName: req.User.FirstName,
LastName: req.User.LastName,
TGBOT: req.User.TgBot,
}
ok, err := a.api.Edit(ctx, user)
if err != nil {
return nil, err
}
return &tgbuser.EditTGBUserResponse{Response: ok}, nil
}
func (a Adapter) Delete(ctx context.Context, req *tgbuser.DeleteTGBUserRequest) (*tgbuser.DeleteTGBUserResponse, error) {
ok, err := a.api.Delete(ctx, req.TgbId)
// TODO db work
return &tgbuser.DeleteTGBUserResponse{Response: ok}, err
}
func (a Adapter) AddBot(ctx context.Context, req *tgbuser.AddBotTGBUserRequest) (*tgbuser.AddBotTGBUserResponse, error) {
// TODO db work
ok, err := a.api.AddBot(ctx, req.TgbId, req.BotName)
return &tgbuser.AddBotTGBUserResponse{Response: ok}, err
}
func (a Adapter) DeleteBot(ctx context.Context, req *tgbuser.DeleteBotTGBUserRequest) (*tgbuser.DeleteBotTGBUserResponse, error) {
// TODO db work
ok, err := a.api.DeleteBot(ctx, req.TgbId, req.BotName)
return &tgbuser.DeleteBotTGBUserResponse{Response: ok}, err
}
func (a Adapter) GetBots(ctx context.Context, req *tgbuser.GetBotsTGBUserRequest) (*tgbuser.GetBotsTGBUserResponse, error) {
bl, err := a.api.GetBots(ctx, req.TgbId)
if err != nil {
return nil, err
}
botList := []*tgbuser.Bot{}
for _, v := range bl {
b := &tgbuser.Bot{
Id: v.ID,
BotName: v.BotName,
Created: v.Created,
}
botList = append(botList, b)
}
return &tgbuser.GetBotsTGBUserResponse{Bots: botList}, nil
}
func (a Adapter) GetAllBotsUsers(ctx context.Context, req *tgbuser.GetAllBotsUsersRequest) (*tgbuser.GetAllBotsUsersResponse, error) {
users, err := a.api.GetAllBotsUsers(ctx, req.BotName)
if err != nil {
return nil, err
}
userList := []*tgbuser.User{}
for _, v := range users {
user := &tgbuser.User{
Id: v.ID,
TgbId: v.TGB_ID,
Username: v.Username,
FirstName: v.FirstName,
LastName: v.LastName,
Created: v.Created,
Edited: v.Edited,
}
userList = append(userList, user)
}
return &tgbuser.GetAllBotsUsersResponse{Users: userList}, err
}
func (a Adapter) GetAllBots(ctx context.Context, req *tgbuser.GetAllBotsRequest) (*tgbuser.GetAllBotsResponse, error) {
bots, err := a.api.GetAllBots(ctx)
if err != nil {
return nil, err
}
botList := []*tgbuser.Bot{}
for _, v := range bots {
bot := &tgbuser.Bot{
Id: v.ID,
BotName: v.BotName,
Created: v.Created,
}
botList = append(botList, bot)
}
return &tgbuser.GetAllBotsResponse{Bots: botList}, nil
}
func (a Adapter) CreateBot(ctx context.Context, req *tgbuser.TGBBotNameRequest) (*tgbuser.TGBGenericResponse, error) {
ok, err := a.api.CreateBot(ctx, req.BotName)
return &tgbuser.TGBGenericResponse{Response: ok}, err
}
func (a Adapter) CreateAccessRequest(ctx context.Context, req *tgbuser.TGBUserBotNameRequest) (*tgbuser.TGBGenericResponse, error) {
ok, err := a.api.CreateAccessRequest(ctx, req.TgbId, req.BotName)
return &tgbuser.TGBGenericResponse{Response: ok}, err
}
func (a Adapter) GrantAccess(ctx context.Context, req *tgbuser.TGBUserBotNameRequest) (*tgbuser.TGBGenericResponse, error) {
ok, err := a.api.GrantAccess(ctx, req.TgbId, req.BotName)
return &tgbuser.TGBGenericResponse{Response: ok}, err
}
func (a Adapter) BanUser(ctx context.Context, req *tgbuser.TGBBanUserRequest) (*tgbuser.TGBGenericResponse, error) {
ok, err := a.api.BanUser(ctx, req.TgbId, req.Until, req.BotName)
return &tgbuser.TGBGenericResponse{Response: ok}, err
}
func (a Adapter) UnBanUser(ctx context.Context, req *tgbuser.TGBUserBotNameRequest) (*tgbuser.TGBGenericResponse, error) {
ok, err := a.api.UnBanUser(ctx, req.TgbId, req.BotName)
return &tgbuser.TGBGenericResponse{Response: ok}, err
}
func (a Adapter) GetAllAccessRequest(ctx context.Context, req *tgbuser.TGBBotNameRequest) (*tgbuser.GetAccessResponse, error) {
ar, err := a.api.GetAllAccessRequest(ctx, req.BotName)
if err != nil {
return nil, err
}
tgbARL := []*tgbuser.AccessRq{}
for _, it := range ar {
tgbAr := &tgbuser.AccessRq{
TgbId: it.TgbID,
BotName: it.BotName,
Created: it.Created,
}
tgbARL = append(tgbARL, tgbAr)
}
return &tgbuser.GetAccessResponse{Access: tgbARL}, err
}
func (a Adapter) GetAllBannedUsers(ctx context.Context, req *tgbuser.TGBBotNameRequest) (*tgbuser.GetBanResponse, error) {
br, err := a.api.GetAllBannedUsers(ctx, req.BotName)
if err != nil {
return nil, err
}
brL := []*tgbuser.Ban{}
for _, it := range br {
b := &tgbuser.Ban{
TgbId: it.TgbID,
BotName: it.BotName,
Until: it.Until,
Created: it.Created,
}
brL = append(brL, b)
}
return &tgbuser.GetBanResponse{Bans: brL}, err
}
func (a Adapter) GetBannedBot(ctx context.Context, req *tgbuser.TGBUserRequest) (*tgbuser.GetBanResponse, error) {
br, err := a.api.GetBannedBot(ctx, req.TgbId)
if err != nil {
return nil, err
}
brL := []*tgbuser.Ban{}
for _, it := range br {
b := &tgbuser.Ban{
TgbId: it.TgbID,
BotName: it.BotName,
Until: it.Until,
Created: it.Created,
}
brL = append(brL, b)
}
return &tgbuser.GetBanResponse{Bans: brL}, err
}
func (a Adapter) GetAccessRequest(ctx context.Context, req *tgbuser.TGBUserRequest) (*tgbuser.GetAccessResponse, error) {
ar, err := a.api.GetAccessRequest(ctx, req.TgbId)
if err != nil {
return nil, err
}
tgbARL := []*tgbuser.AccessRq{}
for _, it := range ar {
tgbAr := &tgbuser.AccessRq{
TgbId: it.TgbID,
BotName: it.BotName,
Created: it.Created,
}
tgbARL = append(tgbARL, tgbAr)
}
return &tgbuser.GetAccessResponse{Access: tgbARL}, err
}