feat(channel): integrate channel functionality into post management
- Added Channel support in Post model, including ChannelID field in PostRequest and PostResponse DTOs. - Updated PostService and PostRepository to handle channel-specific logic for creating and listing posts. - Introduced ChannelRepository and ChannelService, along with corresponding handlers for managing channels. - Enhanced router to include routes for channel management and public channel listing. - Seeded default channels in the database during initialization.
This commit is contained in:
65
internal/service/channel_service.go
Normal file
65
internal/service/channel_service.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/repository"
|
||||
)
|
||||
|
||||
type ChannelService interface {
|
||||
ListActive() ([]*model.Channel, error)
|
||||
ListAll() ([]*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) 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user