package main

import "context"

func GetUserById(ctx context.Context, userId uint32) (*User, error) {
	user, err := CacheGetUserById(userId)
	if err != nil {
		user = &User{Id: userId}
		err = DbGetWholeUser(ctx, user)
		if err != nil {
			return nil, err
		}
	}

	return user, nil
}

func GetUserByToken(ctx context.Context, token string) (*User, error) {
	userId, err := TokenValidateGetId(token)
	if err != nil {
		return nil, err
	}
	return GetUserById(ctx, userId)
}

func GetGroup(ctx context.Context, groupId uint32) (*Group, error) {
	group, err := CacheGetGroup(groupId)
	if err != nil {
		group = &Group{Id: groupId}
		if err = DbGroupGetById(ctx, group); err != nil {
			return nil, err
		}
		if err = DbGroupGetMembers(ctx, group); err != nil {
			return nil, err
		}
		CacheSaveGroup(group)
	}
	return group, nil
}
