feat: entities
This commit is contained in:
66
internal/services/comment.go
Normal file
66
internal/services/comment.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"app/internal/dal"
|
||||
"app/internal/database"
|
||||
"app/internal/models"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CommentService struct {
|
||||
}
|
||||
type Comment = models.Comment
|
||||
|
||||
func (service *CommentService) Create(item Comment) (Comment, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.Comment.Preload(field.Associations).Create(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = AppendAssociations(database.GetInstance(), &item)
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (service *CommentService) GetAll() ([]*Comment, error) {
|
||||
var comments []*Comment
|
||||
comments, err := dal.Comment.Preload(field.Associations).Find()
|
||||
return comments, err
|
||||
}
|
||||
func (service *CommentService) GetById(id uint) (*Comment, error) {
|
||||
item, err := dal.Comment.Preload(field.Associations).Where(dal.Comment.Id.Eq(id)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
func (service *CommentService) Update(item Comment) (Comment, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.Comment.Preload(field.Associations).Save(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
|
||||
err = UpdateAssociations(database.GetInstance(), &item)
|
||||
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
|
||||
return item, err
|
||||
}
|
||||
func (service *CommentService) Delete(id uint) error {
|
||||
_, err := dal.Comment.Unscoped().Where(dal.Comment.Id.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (service *CommentService) Count() (int64, error) {
|
||||
amount, err := dal.Comment.Count()
|
||||
return amount, err
|
||||
}
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
)
|
||||
|
||||
func InsertDefaultData() {
|
||||
insertPostTypes()
|
||||
insertAuthors()
|
||||
insertPosts()
|
||||
insertComments()
|
||||
}
|
||||
|
||||
func InsertDefaultEntityData[T any](service Service[T], entities []T) {
|
||||
@@ -23,22 +25,25 @@ func InsertDefaultEntityData[T any](service Service[T], entities []T) {
|
||||
func insertPosts() {
|
||||
InsertDefaultEntityData(&PostService{}, []Post{
|
||||
{
|
||||
Id: 1,
|
||||
Text: "Жителям Кузбасса запретили болеть.",
|
||||
Deadline: time.Now().Unix(),
|
||||
AuthorId: 1,
|
||||
Id: 1,
|
||||
Text: "Жителям Кузбасса запретили болеть.",
|
||||
Deadline: time.Now().Unix(),
|
||||
AuthorId: 1,
|
||||
PostTypeId: 1,
|
||||
},
|
||||
{
|
||||
Id: 2,
|
||||
Deadline: time.Now().Add(time.Hour * 24 * 5).Unix(),
|
||||
Text: "⚡️⚡️⚡️Дома будут летать.",
|
||||
AuthorId: 2,
|
||||
Id: 2,
|
||||
Deadline: time.Now().Add(time.Hour * 24 * 5).Unix(),
|
||||
Text: "⚡️⚡️⚡️Дома будут летать.",
|
||||
AuthorId: 2,
|
||||
PostTypeId: 2,
|
||||
},
|
||||
{
|
||||
Id: 3,
|
||||
Deadline: time.Now().Add(time.Hour * 24 * 6).Unix(),
|
||||
Text: "В Кузбассе начали строить дома выше, чтобы жители были ближе к богу и солнцу.",
|
||||
AuthorId: 3,
|
||||
Id: 3,
|
||||
Deadline: time.Now().Add(time.Hour * 24 * 6).Unix(),
|
||||
Text: "В Кузбассе начали строить дома выше, чтобы жители были ближе к богу и солнцу.",
|
||||
AuthorId: 3,
|
||||
PostTypeId: 3,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -59,3 +64,43 @@ func insertAuthors() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func insertComments() {
|
||||
InsertDefaultEntityData(&CommentService{}, []Comment{
|
||||
{
|
||||
Id: 1,
|
||||
Text: "Это просто замечательно!",
|
||||
AuthorId: 1,
|
||||
Posts: []Post{{Id: 1}},
|
||||
},
|
||||
{
|
||||
Id: 2,
|
||||
Text: "Я тоже думаю, что это замечательно!",
|
||||
AuthorId: 2,
|
||||
Posts: []Post{{Id: 2}},
|
||||
},
|
||||
{
|
||||
Id: 3,
|
||||
Text: "Я тоже думаю, что это замечательно!",
|
||||
AuthorId: 3,
|
||||
Posts: []Post{{Id: 3}},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func insertPostTypes() {
|
||||
InsertDefaultEntityData(&PostTypeService{}, []PostType{
|
||||
{
|
||||
Id: 1,
|
||||
Name: "Общество",
|
||||
},
|
||||
{
|
||||
Id: 2,
|
||||
Name: "Политика",
|
||||
},
|
||||
{
|
||||
Id: 3,
|
||||
Name: "Экономика",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package services
|
||||
|
||||
import (
|
||||
"app/internal/dal"
|
||||
"app/internal/database"
|
||||
"app/internal/dialogs"
|
||||
excel "app/internal/extras/excel"
|
||||
"app/internal/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -15,7 +17,12 @@ type PostService struct{}
|
||||
type Post = models.Post
|
||||
|
||||
func (service *PostService) Create(item Post) (Post, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.Post.Preload(field.Associations).Create(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = AppendAssociations(database.GetInstance(), &item)
|
||||
return item, err
|
||||
}
|
||||
func (service *PostService) GetAll() ([]*Post, error) {
|
||||
@@ -34,10 +41,20 @@ func (service *PostService) GetById(id uint) (*Post, error) {
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (service *PostService) Update(item Post) (Post, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.Post.Preload(field.Associations).Save(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = UpdateAssociations(database.GetInstance(), &item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (service *PostService) Delete(id uint) error {
|
||||
_, err := dal.Post.Unscoped().Where(dal.Post.Id.Eq(id)).Delete()
|
||||
return err
|
||||
|
||||
61
internal/services/posttype.go
Normal file
61
internal/services/posttype.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"app/internal/dal"
|
||||
"app/internal/database"
|
||||
"app/internal/models"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PostTypeService struct {
|
||||
}
|
||||
type PostType = models.PostType
|
||||
|
||||
func (service *PostTypeService) Create(item PostType) (PostType, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.PostType.Preload(field.Associations).Create(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = AppendAssociations(database.GetInstance(), &item)
|
||||
return item, err
|
||||
}
|
||||
func (service *PostTypeService) GetAll() ([]*PostType, error) {
|
||||
var posttypes []*PostType
|
||||
posttypes, err := dal.PostType.Preload(field.Associations).Find()
|
||||
return posttypes, err
|
||||
}
|
||||
func (service *PostTypeService) GetById(id uint) (*PostType, error) {
|
||||
item, err := dal.PostType.Preload(field.Associations).Where(dal.PostType.Id.Eq(id)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
func (service *PostTypeService) Update(item PostType) (PostType, error) {
|
||||
ReplaceEmptySlicesWithNil(&item)
|
||||
err := dal.PostType.Preload(field.Associations).Save(&item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
err = UpdateAssociations(database.GetInstance(), &item)
|
||||
if err != nil {
|
||||
return item, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
func (service *PostTypeService) Delete(id uint) error {
|
||||
_, err := dal.PostType.Unscoped().Where(dal.PostType.Id.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
func (service *PostTypeService) Count() (int64, error) {
|
||||
amount, err := dal.PostType.Count()
|
||||
return amount, err
|
||||
}
|
||||
@@ -5,4 +5,6 @@ import "github.com/wailsapp/wails/v3/pkg/application"
|
||||
var ExportedServices = []application.Service{
|
||||
application.NewService(&PostService{}),
|
||||
application.NewService(&AuthorService{}),
|
||||
application.NewService(&CommentService{}),
|
||||
application.NewService(&PostTypeService{}),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user