V2-4 : placements multi-device (report_bytes, tables 000015, copies UI)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// PlacementDTO serializes one byte-presence entry for
|
||||
// GET /files/:id/placements. The `server` entry is implicit (derived from
|
||||
// `resources.has_content`, no row) ; device entries come from client reports
|
||||
// (`report_bytes` ops). `updatedAt` is RFC3339 (server entry: resource
|
||||
// timestamp ; device entries: report timestamp).
|
||||
type PlacementDTO struct {
|
||||
LocationType string `json:"locationType"`
|
||||
DeviceID string `json:"deviceId,omitempty"`
|
||||
HasBytes bool `json:"hasBytes"`
|
||||
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||
}
|
||||
|
||||
// ListPlacements returns byte presence of a resource the user OWNS (server
|
||||
// entry + device reports, newest first). Non-owner → ErrNotFound (same
|
||||
// owner-only scoping as shares/links reads).
|
||||
func (s *Resources) ListPlacements(userID, resourceID string) ([]PlacementDTO, error) {
|
||||
ownerID, err := s.Repo.GetResourceOwner(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ownerID != userID {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
row, err := s.Repo.GetFileVisible(userID, resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []PlacementDTO{
|
||||
{
|
||||
LocationType: "server",
|
||||
HasBytes: row.HasContent,
|
||||
UpdatedAt: row.UpdatedAt.UTC().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
placements, err := s.Repository.Placements.ListForResource(resourceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, p := range placements {
|
||||
out = append(out, PlacementDTO{
|
||||
LocationType: p.LocationType,
|
||||
DeviceID: p.DeviceID,
|
||||
HasBytes: p.HasBytes,
|
||||
UpdatedAt: p.UpdatedAt.UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user