- Updated PostHandler to include channel information in post responses. - Introduced PostChannelBrief DTO to represent channel details associated with posts. - Modified post conversion functions to support channel data, ensuring proper mapping of channel IDs to channel names. - Enhanced ChannelRepository and ChannelService to facilitate batch retrieval of channels by IDs. - Updated wire generation to inject channel service into post handler for improved functionality.
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"carrot_bbs/internal/model"
|
|
"carrot_bbs/internal/repository"
|
|
)
|
|
|
|
type ChannelService interface {
|
|
ListActive() ([]*model.Channel, error)
|
|
ListAll() ([]*model.Channel, error)
|
|
MapByIDs(ids []string) (map[string]*model.Channel, error)
|
|
Create(name, slug, description string, sortOrder int, isActive bool) (*model.Channel, error)
|
|
Update(id, name, slug, description string, sortOrder int, isActive bool) (*model.Channel, error)
|
|
Delete(id string) error
|
|
}
|
|
|
|
type channelServiceImpl struct {
|
|
channelRepo *repository.ChannelRepository
|
|
}
|
|
|
|
func NewChannelService(channelRepo *repository.ChannelRepository) ChannelService {
|
|
return &channelServiceImpl{channelRepo: channelRepo}
|
|
}
|
|
|
|
func (s *channelServiceImpl) ListActive() ([]*model.Channel, error) {
|
|
return s.channelRepo.ListActive()
|
|
}
|
|
|
|
func (s *channelServiceImpl) ListAll() ([]*model.Channel, error) {
|
|
return s.channelRepo.ListAll()
|
|
}
|
|
|
|
func (s *channelServiceImpl) MapByIDs(ids []string) (map[string]*model.Channel, error) {
|
|
list, err := s.channelRepo.ListByIDs(ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m := make(map[string]*model.Channel, len(list))
|
|
for _, ch := range list {
|
|
if ch != nil {
|
|
m[ch.ID] = ch
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func (s *channelServiceImpl) Create(name, slug, description string, sortOrder int, isActive bool) (*model.Channel, error) {
|
|
channel := &model.Channel{
|
|
Name: name,
|
|
Slug: slug,
|
|
Description: description,
|
|
SortOrder: sortOrder,
|
|
IsActive: isActive,
|
|
}
|
|
if err := s.channelRepo.Create(channel); err != nil {
|
|
return nil, err
|
|
}
|
|
return channel, nil
|
|
}
|
|
|
|
func (s *channelServiceImpl) Update(id, name, slug, description string, sortOrder int, isActive bool) (*model.Channel, error) {
|
|
channel, err := s.channelRepo.GetByID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
channel.Name = name
|
|
channel.Slug = slug
|
|
channel.Description = description
|
|
channel.SortOrder = sortOrder
|
|
channel.IsActive = isActive
|
|
if err := s.channelRepo.Update(channel); err != nil {
|
|
return nil, err
|
|
}
|
|
return channel, nil
|
|
}
|
|
|
|
func (s *channelServiceImpl) Delete(id string) error {
|
|
return s.channelRepo.Delete(id)
|
|
}
|
|
|