137 lines
5.9 KiB
Go
137 lines
5.9 KiB
Go
package live
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
bizlive "kra/internal/biz/live"
|
|
"kra/internal/service/dto"
|
|
"time"
|
|
)
|
|
|
|
type CatalogService struct {
|
|
categories *bizlive.CategoryUsecase
|
|
rooms *bizlive.RoomUsecase
|
|
}
|
|
|
|
func NewCatalogService(categories *bizlive.CategoryUsecase, rooms *bizlive.RoomUsecase) *CatalogService {
|
|
return &CatalogService{categories: categories, rooms: rooms}
|
|
}
|
|
func liveListOptions(input dto.LiveListRequest) bizlive.ListOptions {
|
|
page, size := input.Page, input.PageSize
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if size < 1 {
|
|
size = 10
|
|
}
|
|
return bizlive.ListOptions{Offset: (page - 1) * size, Limit: size, OrderBy: input.OrderBy}
|
|
}
|
|
func liveDateRange(start, end string) []time.Time {
|
|
if start == "" || end == "" {
|
|
return nil
|
|
}
|
|
from, e1 := time.ParseInLocation("2006-01-02", start, time.Local)
|
|
to, e2 := time.ParseInLocation("2006-01-02", end, time.Local)
|
|
if e1 != nil || e2 != nil {
|
|
return nil
|
|
}
|
|
return []time.Time{from, to.Add(24*time.Hour - time.Nanosecond)}
|
|
}
|
|
func (s *CatalogService) ListCategories(ctx context.Context, input *dto.LiveCategoryListRequest) ([]*dto.LiveCategoryResponse, int64, error) {
|
|
items, total, err := s.categories.List(ctx, bizlive.CategoryFilter{Name: input.Name, Enabled: input.Enabled}, liveListOptions(input.LiveListRequest))
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result := make([]*dto.LiveCategoryResponse, 0, len(items))
|
|
for _, item := range items {
|
|
result = append(result, categoryResponse(item))
|
|
}
|
|
return result, total, nil
|
|
}
|
|
func (s *CatalogService) CreateCategory(ctx context.Context, input *dto.LiveCategoryRequest) (*dto.LiveCategoryResponse, error) {
|
|
item, err := s.categories.Create(ctx, &bizlive.Category{Name: input.Name, Icon: input.Icon, Sort: input.Sort, Enabled: input.Enabled})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return categoryResponse(item), nil
|
|
}
|
|
func (s *CatalogService) UpdateCategory(ctx context.Context, id uint, input *dto.LiveCategoryRequest) (*dto.LiveCategoryResponse, error) {
|
|
item, err := s.categories.Update(ctx, &bizlive.Category{ID: id, Name: input.Name, Icon: input.Icon, Sort: input.Sort, Enabled: input.Enabled})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return categoryResponse(item), nil
|
|
}
|
|
func (s *CatalogService) DeleteCategory(ctx context.Context, id uint) error {
|
|
return s.categories.Delete(ctx, id)
|
|
}
|
|
func (s *CatalogService) ListRooms(ctx context.Context, input *dto.LiveRoomListRequest) ([]*dto.LiveRoomResponse, int64, error) {
|
|
items, total, err := s.rooms.List(ctx, bizlive.RoomFilter{Name: input.Name, AnchorName: input.AnchorName, CategoryID: input.CategoryID, Status: input.Status, Enabled: input.Enabled, CreatedAt: liveDateRange(input.StartedAt, input.EndedAt)}, liveListOptions(input.LiveListRequest))
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result := make([]*dto.LiveRoomResponse, 0, len(items))
|
|
for _, item := range items {
|
|
result = append(result, roomResponse(item))
|
|
}
|
|
return result, total, nil
|
|
}
|
|
func (s *CatalogService) ListPublicRooms(ctx context.Context, input *dto.LiveRoomListRequest) ([]*dto.LiveRoomResponse, int64, error) {
|
|
enabled := true
|
|
items, total, err := s.rooms.List(ctx, bizlive.RoomFilter{Name: input.Name, CategoryID: input.CategoryID, Status: bizlive.RoomStatusLive, Enabled: &enabled, CategoryEnabled: &enabled}, liveListOptions(input.LiveListRequest))
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result := make([]*dto.LiveRoomResponse, 0, len(items))
|
|
for _, item := range items {
|
|
result = append(result, roomResponse(item))
|
|
}
|
|
return result, total, nil
|
|
}
|
|
func (s *CatalogService) GetRoom(ctx context.Context, id uint) (*dto.LiveRoomResponse, error) {
|
|
item, err := s.rooms.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return roomResponse(item), nil
|
|
}
|
|
func (s *CatalogService) GetPublicRoom(ctx context.Context, id uint) (*dto.LiveRoomResponse, error) {
|
|
item, err := s.rooms.GetPublic(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return roomResponse(item), nil
|
|
}
|
|
func liveRoomDO(id uint, input *dto.LiveRoomRequest) *bizlive.Room {
|
|
return &bizlive.Room{ID: id, Name: input.Name, CategoryID: input.CategoryID, CoverURL: input.CoverURL, Intro: input.Intro, Sort: input.Sort, AnchorUserID: input.AnchorUserID, StreamURL: input.StreamURL, ReplayURL: input.ReplayURL, Enabled: input.Enabled}
|
|
}
|
|
func (s *CatalogService) CreateRoom(ctx context.Context, input *dto.LiveRoomRequest) (*dto.LiveRoomResponse, error) {
|
|
item, err := s.rooms.Create(ctx, liveRoomDO(0, input))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return roomResponse(item), nil
|
|
}
|
|
func (s *CatalogService) UpdateRoom(ctx context.Context, id uint, input *dto.LiveRoomRequest) (*dto.LiveRoomResponse, error) {
|
|
item, err := s.rooms.Update(ctx, liveRoomDO(id, input))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return roomResponse(item), nil
|
|
}
|
|
func (s *CatalogService) DeleteRoom(ctx context.Context, id uint) error {
|
|
return s.rooms.Delete(ctx, id)
|
|
}
|
|
func (s *CatalogService) SetRoomEnabled(ctx context.Context, id uint, enabled bool) error {
|
|
return s.rooms.SetEnabled(ctx, id, enabled)
|
|
}
|
|
func (s *CatalogService) SaveRoomSort(ctx context.Context, values map[uint]int) error {
|
|
return s.rooms.SaveSort(ctx, values)
|
|
}
|
|
func categoryResponse(item *bizlive.Category) *dto.LiveCategoryResponse {
|
|
return &dto.LiveCategoryResponse{ID: item.ID, Name: item.Name, Icon: item.Icon, Sort: item.Sort, Enabled: item.Enabled, RoomCount: item.RoomCount, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt}
|
|
}
|
|
func roomResponse(item *bizlive.Room) *dto.LiveRoomResponse {
|
|
return &dto.LiveRoomResponse{ID: item.ID, StreamID: fmt.Sprintf("room-%d", item.ID), Name: item.Name, CategoryID: item.CategoryID, CategoryName: item.CategoryName, CoverURL: item.CoverURL, Intro: item.Intro, Sort: item.Sort, AnchorUserID: item.AnchorUserID, AnchorName: item.AnchorName, AnchorAvatar: item.AnchorAvatar, StreamURL: item.StreamURL, ReplayURL: item.ReplayURL, Enabled: item.Enabled, Status: item.Status, ViewerCount: item.ViewerCount, ViewerCountText: item.ViewerCountText, LikeCount: item.LikeCount, StartedAt: item.StartedAt, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt}
|
|
}
|