feat: import services and standardize Go 1.26.5
This commit is contained in:
62
apps/base/cloud/internal/logic/album/create_album.go
Normal file
62
apps/base/cloud/internal/logic/album/create_album.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
)
|
||||
|
||||
// 创建相册
|
||||
func CreateAlbum(ctx context.Context, in *pb.CreateAlbumRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valid code
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.CloudIdentity) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
record := models.CloudAlbum{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
},
|
||||
Std_Passport: types.Std_Passport{
|
||||
PassportID: auth.ID,
|
||||
PassportIdentity: auth.Identity,
|
||||
},
|
||||
CloudBase: models.CloudBase{
|
||||
CloudID: uint(in.CloudId),
|
||||
CloudIdentity: in.CloudIdentity,
|
||||
},
|
||||
Name: in.Name,
|
||||
Description: in.Description,
|
||||
CoverPhoto: in.CoverPhoto,
|
||||
IsPrivate: in.IsPrivate,
|
||||
}
|
||||
|
||||
if err := impl.DBService.Create(&record).Error; err != nil {
|
||||
printer.Error("Create album error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: record.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
60
apps/base/cloud/internal/logic/album/delete_album.go
Normal file
60
apps/base/cloud/internal/logic/album/delete_album.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 删除相册
|
||||
func DeleteAlbum(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var album models.CloudAlbum
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(&album).Error; err != nil {
|
||||
printer.Error("Album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 删除相册下的所有照片
|
||||
if err := impl.DBService.Where("album_id = ?", album.ID).Delete(&models.CloudPhoto{}).Error; err != nil {
|
||||
printer.Error("Delete photos error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 删除相册
|
||||
if err := impl.DBService.Delete(&album).Error; err != nil {
|
||||
printer.Error("Delete album error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
55
apps/base/cloud/internal/logic/album/delete_photo.go
Normal file
55
apps/base/cloud/internal/logic/album/delete_photo.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 删除照片
|
||||
func DeletePhoto(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var photo models.CloudPhoto
|
||||
query := impl.DBService.Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_albums.passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("cloud_photos.id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("cloud_photos.identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(&photo).Error; err != nil {
|
||||
printer.Error("Photo not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 删除照片
|
||||
if err := impl.DBService.Delete(&photo).Error; err != nil {
|
||||
printer.Error("Delete photo error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
78
apps/base/cloud/internal/logic/album/get_album.go
Normal file
78
apps/base/cloud/internal/logic/album/get_album.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取相册详情
|
||||
func GetAlbum(ctx context.Context, in *pb.IdentRequest) (reply *pb.CloudAlbumItem, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var album models.CloudAlbum
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.Preload("Photos").First(&album).Error; err != nil {
|
||||
printer.Error("Album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 转换照片数据
|
||||
var photos []*pb.CloudPhotoItem
|
||||
for _, photo := range album.Photos {
|
||||
photos = append(photos, &pb.CloudPhotoItem{
|
||||
Id: uint64(photo.ID),
|
||||
Identity: photo.Identity,
|
||||
AlbumId: uint64(photo.AlbumID),
|
||||
Title: photo.Title,
|
||||
Description: photo.Description,
|
||||
FilePath: photo.FilePath,
|
||||
FileSize: photo.FileSize,
|
||||
MimeType: photo.MimeType,
|
||||
Width: int32(photo.Width),
|
||||
Height: int32(photo.Height),
|
||||
TakenAt: photo.TakenAt.Format(time.RFC3339),
|
||||
Location: photo.Location,
|
||||
Tags: photo.Tags,
|
||||
CreatedAt: photo.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.UpdatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.CloudAlbumItem{
|
||||
Id: uint64(album.ID),
|
||||
Identity: album.Identity,
|
||||
Name: album.Name,
|
||||
Description: album.Description,
|
||||
CoverPhoto: album.CoverPhoto,
|
||||
IsPrivate: album.IsPrivate,
|
||||
CreatedAt: album.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: album.UpdatedAt.Format(time.RFC3339),
|
||||
Photos: photos,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
76
apps/base/cloud/internal/logic/album/get_photo.go
Normal file
76
apps/base/cloud/internal/logic/album/get_photo.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取照片详情
|
||||
func GetPhoto(ctx context.Context, in *pb.IdentRequest) (reply *pb.CloudPhotoItem, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var photo models.CloudPhoto
|
||||
query := impl.DBService.Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_albums.passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("cloud_photos.id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("cloud_photos.identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.Preload("Album").First(&photo).Error; err != nil {
|
||||
printer.Error("Photo not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 转换相册数据
|
||||
albumItem := &pb.CloudAlbumItem{
|
||||
Id: uint64(photo.Album.ID),
|
||||
Identity: photo.Album.Identity,
|
||||
Name: photo.Album.Name,
|
||||
Description: photo.Album.Description,
|
||||
CoverPhoto: photo.Album.CoverPhoto,
|
||||
IsPrivate: photo.Album.IsPrivate,
|
||||
CreatedAt: photo.Album.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.Album.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
reply = &pb.CloudPhotoItem{
|
||||
Id: uint64(photo.ID),
|
||||
Identity: photo.Identity,
|
||||
AlbumId: uint64(photo.AlbumID),
|
||||
Title: photo.Title,
|
||||
Description: photo.Description,
|
||||
FilePath: photo.FilePath,
|
||||
FileSize: photo.FileSize,
|
||||
MimeType: photo.MimeType,
|
||||
Width: int32(photo.Width),
|
||||
Height: int32(photo.Height),
|
||||
TakenAt: photo.TakenAt.Format(time.RFC3339),
|
||||
Location: photo.Location,
|
||||
Tags: photo.Tags,
|
||||
CreatedAt: photo.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.UpdatedAt.Format(time.RFC3339),
|
||||
Album: albumItem,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
92
apps/base/cloud/internal/logic/album/list_albums.go
Normal file
92
apps/base/cloud/internal/logic/album/list_albums.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取相册列表
|
||||
func ListAlbums(ctx context.Context, in *pb.FetchRequest) (reply *pb.ListAlbumsResponse, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request page_no,page_size.
|
||||
if in.GetPageNo() < 1 {
|
||||
in.PageNo = 1
|
||||
}
|
||||
if in.GetPageSize() < 10 {
|
||||
in.PageSize = 50
|
||||
}
|
||||
|
||||
// logic code
|
||||
var albums []models.CloudAlbum
|
||||
var total int64
|
||||
|
||||
// 获取总数
|
||||
if err := impl.DBService.Model(&models.CloudAlbum{}).Where("passport_id = ?", auth.ID).Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (in.PageNo - 1) * in.PageSize
|
||||
if err := impl.DBService.Where("passport_id = ?", auth.ID).
|
||||
Preload("Photos").
|
||||
Order("created_at DESC").
|
||||
Offset(int(offset)).
|
||||
Limit(int(in.PageSize)).
|
||||
Find(&albums).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 转换数据
|
||||
var albumItems []*pb.CloudAlbumItem
|
||||
for _, album := range albums {
|
||||
var photos []*pb.CloudPhotoItem
|
||||
for _, photo := range album.Photos {
|
||||
photos = append(photos, &pb.CloudPhotoItem{
|
||||
Id: uint64(photo.ID),
|
||||
Identity: photo.Identity,
|
||||
AlbumId: uint64(photo.AlbumID),
|
||||
Title: photo.Title,
|
||||
Description: photo.Description,
|
||||
FilePath: photo.FilePath,
|
||||
FileSize: photo.FileSize,
|
||||
MimeType: photo.MimeType,
|
||||
Width: int32(photo.Width),
|
||||
Height: int32(photo.Height),
|
||||
TakenAt: photo.TakenAt.Format(time.RFC3339),
|
||||
Location: photo.Location,
|
||||
Tags: photo.Tags,
|
||||
CreatedAt: photo.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.UpdatedAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
|
||||
albumItems = append(albumItems, &pb.CloudAlbumItem{
|
||||
Id: uint64(album.ID),
|
||||
Identity: album.Identity,
|
||||
Name: album.Name,
|
||||
Description: album.Description,
|
||||
CoverPhoto: album.CoverPhoto,
|
||||
IsPrivate: album.IsPrivate,
|
||||
CreatedAt: album.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: album.UpdatedAt.Format(time.RFC3339),
|
||||
Photos: photos,
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.ListAlbumsResponse{
|
||||
Albums: albumItems,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
94
apps/base/cloud/internal/logic/album/list_photos.go
Normal file
94
apps/base/cloud/internal/logic/album/list_photos.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 获取照片列表
|
||||
func ListPhotos(ctx context.Context, in *pb.FetchRequest) (reply *pb.ListPhotosResponse, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request page_no,page_size.
|
||||
if in.GetPageNo() < 1 {
|
||||
in.PageNo = 1
|
||||
}
|
||||
if in.GetPageSize() < 10 {
|
||||
in.PageSize = 50
|
||||
}
|
||||
|
||||
// logic code
|
||||
var photos []models.CloudPhoto
|
||||
var total int64
|
||||
|
||||
// 获取总数
|
||||
if err := impl.DBService.Model(&models.CloudPhoto{}).
|
||||
Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_albums.passport_id = ?", auth.ID).
|
||||
Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (in.PageNo - 1) * in.PageSize
|
||||
if err := impl.DBService.Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_albums.passport_id = ?", auth.ID).
|
||||
Preload("Album").
|
||||
Order("cloud_photos.created_at DESC").
|
||||
Offset(int(offset)).
|
||||
Limit(int(in.PageSize)).
|
||||
Find(&photos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 转换数据
|
||||
var photoItems []*pb.CloudPhotoItem
|
||||
for _, photo := range photos {
|
||||
// 转换相册数据
|
||||
albumItem := &pb.CloudAlbumItem{
|
||||
Id: uint64(photo.Album.ID),
|
||||
Identity: photo.Album.Identity,
|
||||
Name: photo.Album.Name,
|
||||
Description: photo.Album.Description,
|
||||
CoverPhoto: photo.Album.CoverPhoto,
|
||||
IsPrivate: photo.Album.IsPrivate,
|
||||
CreatedAt: photo.Album.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.Album.UpdatedAt.Format(time.RFC3339),
|
||||
}
|
||||
|
||||
photoItems = append(photoItems, &pb.CloudPhotoItem{
|
||||
Id: uint64(photo.ID),
|
||||
Identity: photo.Identity,
|
||||
AlbumId: uint64(photo.AlbumID),
|
||||
Title: photo.Title,
|
||||
Description: photo.Description,
|
||||
FilePath: photo.FilePath,
|
||||
FileSize: photo.FileSize,
|
||||
MimeType: photo.MimeType,
|
||||
Width: int32(photo.Width),
|
||||
Height: int32(photo.Height),
|
||||
TakenAt: photo.TakenAt.Format(time.RFC3339),
|
||||
Location: photo.Location,
|
||||
Tags: photo.Tags,
|
||||
CreatedAt: photo.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: photo.UpdatedAt.Format(time.RFC3339),
|
||||
Album: albumItem,
|
||||
})
|
||||
}
|
||||
|
||||
reply = &pb.ListPhotosResponse{
|
||||
Photos: photoItems,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
60
apps/base/cloud/internal/logic/album/move_photo.go
Normal file
60
apps/base/cloud/internal/logic/album/move_photo.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 移动照片到其他相册
|
||||
func MovePhoto(ctx context.Context, in *pb.MovePhotoRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.PhotoId == 0 || in.NewAlbumId == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
// 验证照片是否存在且属于当前用户
|
||||
var photo models.CloudPhoto
|
||||
if err := impl.DBService.Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_photos.id = ? AND cloud_albums.passport_id = ?", in.PhotoId, auth.ID).
|
||||
First(&photo).Error; err != nil {
|
||||
printer.Error("Photo not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 验证目标相册是否存在且属于当前用户
|
||||
var newAlbum models.CloudAlbum
|
||||
if err := impl.DBService.Where("id = ? AND passport_id = ?", in.NewAlbumId, auth.ID).First(&newAlbum).Error; err != nil {
|
||||
printer.Error("Target album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新照片的相册ID
|
||||
photo.AlbumID = uint(in.NewAlbumId)
|
||||
photo.CloudID = newAlbum.CloudID
|
||||
photo.CloudIdentity = newAlbum.CloudIdentity
|
||||
|
||||
if err := impl.DBService.Save(&photo).Error; err != nil {
|
||||
printer.Error("Move photo error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
55
apps/base/cloud/internal/logic/album/set_cover_photo.go
Normal file
55
apps/base/cloud/internal/logic/album/set_cover_photo.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 设置封面照片
|
||||
func SetCoverPhoto(ctx context.Context, in *pb.SetCoverPhotoRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.AlbumId == 0 || in.PhotoId == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
// 验证相册是否存在且属于当前用户
|
||||
var album models.CloudAlbum
|
||||
if err := impl.DBService.Where("id = ? AND passport_id = ?", in.AlbumId, auth.ID).First(&album).Error; err != nil {
|
||||
printer.Error("Album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 验证照片是否存在且属于该相册
|
||||
var photo models.CloudPhoto
|
||||
if err := impl.DBService.Where("id = ? AND album_id = ?", in.PhotoId, in.AlbumId).First(&photo).Error; err != nil {
|
||||
printer.Error("Photo not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新相册的封面照片
|
||||
album.CoverPhoto = photo.FilePath
|
||||
if err := impl.DBService.Save(&album).Error; err != nil {
|
||||
printer.Error("Set cover photo error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
63
apps/base/cloud/internal/logic/album/update_album.go
Normal file
63
apps/base/cloud/internal/logic/album/update_album.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 更新相册
|
||||
func UpdateAlbum(ctx context.Context, in *pb.CloudAlbumItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var album models.CloudAlbum
|
||||
query := impl.DBService.Where("passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(&album).Error; err != nil {
|
||||
printer.Error("Album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
album.Name = in.Name
|
||||
album.Description = in.Description
|
||||
album.CoverPhoto = in.CoverPhoto
|
||||
album.IsPrivate = in.IsPrivate
|
||||
|
||||
if err := impl.DBService.Save(&album).Error; err != nil {
|
||||
printer.Error("Update album error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
67
apps/base/cloud/internal/logic/album/update_photo.go
Normal file
67
apps/base/cloud/internal/logic/album/update_photo.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 更新照片
|
||||
func UpdatePhoto(ctx context.Context, in *pb.CloudPhotoItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
var photo models.CloudPhoto
|
||||
query := impl.DBService.Joins("JOIN cloud_albums ON cloud_photos.album_id = cloud_albums.id").
|
||||
Where("cloud_albums.passport_id = ?", auth.ID)
|
||||
|
||||
if in.Id > 0 {
|
||||
query = query.Where("cloud_photos.id = ?", in.Id)
|
||||
} else {
|
||||
query = query.Where("cloud_photos.identity = ?", in.Identity)
|
||||
}
|
||||
|
||||
if err := query.First(&photo).Error; err != nil {
|
||||
printer.Error("Photo not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
photo.Title = in.Title
|
||||
photo.Description = in.Description
|
||||
photo.Location = in.Location
|
||||
photo.Tags = in.Tags
|
||||
|
||||
// 更新拍摄时间
|
||||
if in.TakenAt != "" {
|
||||
if t, err := time.Parse(time.RFC3339, in.TakenAt); err == nil {
|
||||
photo.TakenAt = t
|
||||
}
|
||||
}
|
||||
|
||||
if err := impl.DBService.Save(&photo).Error; err != nil {
|
||||
printer.Error("Update photo error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
84
apps/base/cloud/internal/logic/album/upload_photo.go
Normal file
84
apps/base/cloud/internal/logic/album/upload_photo.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package album
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cloud/internal/impl"
|
||||
"git.apinb.com/bsm-apps/cloud/internal/models"
|
||||
pb "git.apinb.com/bsm-apps/cloud/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/engine/types"
|
||||
)
|
||||
|
||||
// 上传照片
|
||||
func UploadPhoto(ctx context.Context, in *pb.CloudPhotoItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// validate request
|
||||
if in.AlbumId == 0 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if strings.TrimSpace(in.FilePath) == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// logic code
|
||||
// 验证相册是否存在且属于当前用户
|
||||
var album models.CloudAlbum
|
||||
if err := impl.DBService.Where("id = ? AND passport_id = ?", in.AlbumId, auth.ID).First(&album).Error; err != nil {
|
||||
printer.Error("Album not found: %v", err)
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 解析拍摄时间
|
||||
var takenAt time.Time
|
||||
if in.TakenAt != "" {
|
||||
if t, err := time.Parse(time.RFC3339, in.TakenAt); err == nil {
|
||||
takenAt = t
|
||||
} else {
|
||||
takenAt = time.Now()
|
||||
}
|
||||
} else {
|
||||
takenAt = time.Now()
|
||||
}
|
||||
|
||||
record := models.CloudPhoto{
|
||||
Std_IICUDS: types.Std_IICUDS{
|
||||
Identity: utils.UUID(),
|
||||
},
|
||||
CloudBase: models.CloudBase{
|
||||
CloudID: album.CloudID,
|
||||
CloudIdentity: album.CloudIdentity,
|
||||
},
|
||||
AlbumID: uint(in.AlbumId),
|
||||
Title: in.Title,
|
||||
Description: in.Description,
|
||||
FilePath: in.FilePath,
|
||||
FileSize: in.FileSize,
|
||||
MimeType: in.MimeType,
|
||||
Width: int(in.Width),
|
||||
Height: int(in.Height),
|
||||
TakenAt: takenAt,
|
||||
Location: in.Location,
|
||||
Tags: in.Tags,
|
||||
}
|
||||
|
||||
if err := impl.DBService.Create(&record).Error; err != nil {
|
||||
printer.Error("Create photo error: %v", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: record.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user