Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/backend/api #105

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ RUN echo "DSN=$DSN" > .env \

RUN go mod download

RUN go mod download

ARG VERS="3.11.4"
ARG ARCH="linux-x86_64"

Expand Down
10 changes: 5 additions & 5 deletions backend/interfaces/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type childServiceServer struct {
interactor *child.Interactor
}

// GetChildListByNurseryID implements where_child_busv1.ChildServiceServer.
func (s *childServiceServer) GetChildListByNurseryID(req *pb.GetChildListByNurseryIDRequest, stream pb.ChildService_GetChildListByNurseryIDServer) error {
return s.interactor.GetChildListByNurseryID(req, stream)
}

func NewChildServiceServer(interactor *child.Interactor) pb.ChildServiceServer {
return &childServiceServer{interactor}
}
Expand All @@ -29,8 +34,3 @@ func (s *childServiceServer) CreateChild(ctx context.Context, req *pb.CreateChil
func (s *childServiceServer) GetChildListByGuardianID(ctx context.Context, req *pb.GetChildListByGuardianIDRequest) (*pb.GetChildListByGuardianIDResponse, error) {
return s.interactor.GetChildListByGuardianID(ctx, req)
}

// GetChildListByNurseryID implements where_child_busv1.ChildServiceServer.
func (s *childServiceServer) GetChildListByNurseryID(ctx context.Context, req *pb.GetChildListByNurseryIDRequest) (*pb.GetChildListByNurseryIDResponse, error) {
return s.interactor.GetChildListByNurseryID(ctx, req)
}
158 changes: 79 additions & 79 deletions backend/proto-gen/go/where_child_bus/v1/child.pb.go

Large diffs are not rendered by default.

84 changes: 56 additions & 28 deletions backend/proto-gen/go/where_child_bus/v1/child_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 19 additions & 21 deletions backend/usecases/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ func (i *Interactor) GetChildListByGuardianID(ctx context.Context, req *pb.GetCh
}, nil
}

func (i *Interactor) GetChildListByNurseryID(ctx context.Context, req *pb.GetChildListByNurseryIDRequest) (*pb.GetChildListByNurseryIDResponse, error) {
func (i *Interactor) GetChildListByNurseryID(req *pb.GetChildListByNurseryIDRequest, stream pb.ChildService_GetChildListByNurseryIDServer) error {
nurseryID, err := uuid.Parse(req.NurseryId)
if err != nil {
i.logger.Error("failed to parse nursery ID", "error", err)
return nil, err
return err
}

// 子供と保育園の間に親を介在させるためのクエリを修正
children, err := i.getChildList(ctx, func(tx *ent.Tx) (*ent.ChildQuery, error) {
children, err := i.getChildList(context.Background(), func(tx *ent.Tx) (*ent.ChildQuery, error) {
// 子供のクエリを作成する際に、親を介した保育園の条件を追加
return tx.Child.Query().
Where(childRepo.HasGuardianWith(guardianRepo.HasNurseryWith(nurseryRepo.IDEQ(nurseryID)))).
Expand All @@ -230,43 +230,41 @@ func (i *Interactor) GetChildListByNurseryID(ctx context.Context, req *pb.GetChi

if err != nil {
i.logger.Error("failed to get children by nursery ID", "error", err)
return nil, err
return err
}

// 子供の写真を取得
var photos []*pb.ChildPhoto
for _, child := range children {
// 子供の写真のメタデータを取得
childPhotoRecordList, err := i.entClient.ChildPhoto.Query().
Where(childPhotoRepo.HasChildWith(childRepo.IDEQ(uuid.MustParse(child.Id)))).All(ctx)
Where(childPhotoRepo.HasChildWith(childRepo.IDEQ(uuid.MustParse(child.Id)))).
WithChild().
All(context.Background())

if err != nil {
i.logger.Error("failed to get child photo list", "error", err)
return nil, err
return err
}

// 写真メタデータリストをループ
for _, photoMetadata := range childPhotoRecordList {
// GCSから写真を取得するためのIDを使用
photo_data, err := i.getPhotoFromGCS(ctx, nurseryID.String(), child.Id, photoMetadata.ID.String())
photo_data, err := i.getPhotoFromGCS(context.Background(), nurseryID.String(), child.Id, photoMetadata.ID.String())
if err != nil {
i.logger.Error("failed to get photo from GCS", "error", err)
return nil, err
return err
}

// 結果リストに追加
photos = append(photos, &pb.ChildPhoto{
ChildId: child.Id,
Id: photoMetadata.ID.String(), // 修正: GCSから取得した写真のIDではなく、メタデータのIDを使用
PhotoData: photo_data,
})
if err := stream.Send(&pb.GetChildListByNurseryIDResponse{
Child: child,
Photo: utils.ToPbChildPhoto(photoMetadata, photo_data),
}); err != nil {
i.logger.Error("failed to send child photo", "error", err)
return err
}
}
}

return &pb.GetChildListByNurseryIDResponse{
Children: children,
Photos: photos,
}, nil
return nil
}

func (i *Interactor) GetChildListByBusID(ctx context.Context, req *pb.GetChildListByBusIDRequest) (*pb.GetChildListByBusIDResponse, error) {
Expand Down Expand Up @@ -361,10 +359,10 @@ func (i *Interactor) getChildList(ctx context.Context, queryFunc func(*ent.Tx) (
func (i *Interactor) getPhotoFromGCS(ctx context.Context, nurseryID, childID, photoID string) ([]byte, error) {
// Cloud Storage上の写真のパスを生成
objectName := fmt.Sprintf("%s/%s/raw/%s.png", nurseryID, childID, photoID)

// 指定したバケット内のオブジェクトを取得
rc, err := i.StorageClient.Bucket(i.BucketName).Object(objectName).NewReader(ctx)
if err != nil {
i.logger.Error("failed to get photo from GCS", "error", err)
return nil, err
}
defer rc.Close()
Expand Down
10 changes: 10 additions & 0 deletions backend/usecases/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ func ToPbStation(t *ent.Station, morningNextStationID, eveningNextStationID stri
}
}

func ToPbChildPhoto(t *ent.ChildPhoto, photo_data []byte) *pb.ChildPhoto {
return &pb.ChildPhoto{
Id: t.ID.String(),
ChildId: t.Edges.Child.ID.String(),
PhotoData: photo_data,
CreatedAt: &timestamppb.Timestamp{Seconds: t.CreatedAt.Unix()},
UpdatedAt: &timestamppb.Timestamp{Seconds: t.UpdatedAt.Unix()},
}
}

func HashPassword(password string) (string, error) {
// 環境変数からペッパーを取得
config, _ := config.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ class GetChildListByNurseryIDRequest extends $pb.GeneratedMessage {

class GetChildListByNurseryIDResponse extends $pb.GeneratedMessage {
factory GetChildListByNurseryIDResponse({
$core.Iterable<$8.Child>? children,
$core.Iterable<$8.ChildPhoto>? photos,
$8.Child? child,
$8.ChildPhoto? photo,
}) {
final $result = create();
if (children != null) {
$result.children.addAll(children);
if (child != null) {
$result.child = child;
}
if (photos != null) {
$result.photos.addAll(photos);
if (photo != null) {
$result.photo = photo;
}
return $result;
}
Expand All @@ -251,8 +251,8 @@ class GetChildListByNurseryIDResponse extends $pb.GeneratedMessage {
factory GetChildListByNurseryIDResponse.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);

static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'GetChildListByNurseryIDResponse', package: const $pb.PackageName(_omitMessageNames ? '' : 'where_child_bus.v1'), createEmptyInstance: create)
..pc<$8.Child>(1, _omitFieldNames ? '' : 'children', $pb.PbFieldType.PM, subBuilder: $8.Child.create)
..pc<$8.ChildPhoto>(2, _omitFieldNames ? '' : 'photos', $pb.PbFieldType.PM, subBuilder: $8.ChildPhoto.create)
..aOM<$8.Child>(1, _omitFieldNames ? '' : 'child', subBuilder: $8.Child.create)
..aOM<$8.ChildPhoto>(2, _omitFieldNames ? '' : 'photo', subBuilder: $8.ChildPhoto.create)
..hasRequiredFields = false
;

Expand All @@ -278,10 +278,26 @@ class GetChildListByNurseryIDResponse extends $pb.GeneratedMessage {
static GetChildListByNurseryIDResponse? _defaultInstance;

@$pb.TagNumber(1)
$core.List<$8.Child> get children => $_getList(0);
$8.Child get child => $_getN(0);
@$pb.TagNumber(1)
set child($8.Child v) { setField(1, v); }
@$pb.TagNumber(1)
$core.bool hasChild() => $_has(0);
@$pb.TagNumber(1)
void clearChild() => clearField(1);
@$pb.TagNumber(1)
$8.Child ensureChild() => $_ensure(0);

@$pb.TagNumber(2)
$core.List<$8.ChildPhoto> get photos => $_getList(1);
$8.ChildPhoto get photo => $_getN(1);
@$pb.TagNumber(2)
set photo($8.ChildPhoto v) { setField(2, v); }
@$pb.TagNumber(2)
$core.bool hasPhoto() => $_has(1);
@$pb.TagNumber(2)
void clearPhoto() => clearField(2);
@$pb.TagNumber(2)
$8.ChildPhoto ensurePhoto() => $_ensure(1);
}

class GetChildListByGuardianIDRequest extends $pb.GeneratedMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class ChildServiceClient extends $grpc.Client {
return $createUnaryCall(_$createChild, request, options: options);
}

$grpc.ResponseFuture<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($1.GetChildListByNurseryIDRequest request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$getChildListByNurseryID, request, options: options);
$grpc.ResponseStream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($1.GetChildListByNurseryIDRequest request, {$grpc.CallOptions? options}) {
return $createStreamingCall(_$getChildListByNurseryID, $async.Stream.fromIterable([request]), options: options);
}

$grpc.ResponseFuture<$1.GetChildListByGuardianIDResponse> getChildListByGuardianID($1.GetChildListByGuardianIDRequest request, {$grpc.CallOptions? options}) {
Expand Down Expand Up @@ -77,7 +77,7 @@ abstract class ChildServiceBase extends $grpc.Service {
'GetChildListByNurseryID',
getChildListByNurseryID_Pre,
false,
false,
true,
($core.List<$core.int> value) => $1.GetChildListByNurseryIDRequest.fromBuffer(value),
($1.GetChildListByNurseryIDResponse value) => value.writeToBuffer()));
$addMethod($grpc.ServiceMethod<$1.GetChildListByGuardianIDRequest, $1.GetChildListByGuardianIDResponse>(
Expand All @@ -100,8 +100,8 @@ abstract class ChildServiceBase extends $grpc.Service {
return createChild(call, await request);
}

$async.Future<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID_Pre($grpc.ServiceCall call, $async.Future<$1.GetChildListByNurseryIDRequest> request) async {
return getChildListByNurseryID(call, await request);
$async.Stream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID_Pre($grpc.ServiceCall call, $async.Future<$1.GetChildListByNurseryIDRequest> request) async* {
yield* getChildListByNurseryID(call, await request);
}

$async.Future<$1.GetChildListByGuardianIDResponse> getChildListByGuardianID_Pre($grpc.ServiceCall call, $async.Future<$1.GetChildListByGuardianIDRequest> request) async {
Expand All @@ -113,7 +113,7 @@ abstract class ChildServiceBase extends $grpc.Service {
}

$async.Future<$1.CreateChildResponse> createChild($grpc.ServiceCall call, $1.CreateChildRequest request);
$async.Future<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($grpc.ServiceCall call, $1.GetChildListByNurseryIDRequest request);
$async.Stream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($grpc.ServiceCall call, $1.GetChildListByNurseryIDRequest request);
$async.Future<$1.GetChildListByGuardianIDResponse> getChildListByGuardianID($grpc.ServiceCall call, $1.GetChildListByGuardianIDRequest request);
$async.Future<$1.GetChildListByBusIDResponse> getChildListByBusID($grpc.ServiceCall call, $1.GetChildListByBusIDRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ final $typed_data.Uint8List getChildListByNurseryIDRequestDescriptor = $convert.
const GetChildListByNurseryIDResponse$json = {
'1': 'GetChildListByNurseryIDResponse',
'2': [
{'1': 'children', '3': 1, '4': 3, '5': 11, '6': '.where_child_bus.v1.Child', '10': 'children'},
{'1': 'photos', '3': 2, '4': 3, '5': 11, '6': '.where_child_bus.v1.ChildPhoto', '10': 'photos'},
{'1': 'child', '3': 1, '4': 1, '5': 11, '6': '.where_child_bus.v1.Child', '10': 'child'},
{'1': 'photo', '3': 2, '4': 1, '5': 11, '6': '.where_child_bus.v1.ChildPhoto', '10': 'photo'},
],
};

/// Descriptor for `GetChildListByNurseryIDResponse`. Decode as a `google.protobuf.DescriptorProto`.
final $typed_data.Uint8List getChildListByNurseryIDResponseDescriptor = $convert.base64Decode(
'Ch9HZXRDaGlsZExpc3RCeU51cnNlcnlJRFJlc3BvbnNlEjUKCGNoaWxkcmVuGAEgAygLMhkud2'
'hlcmVfY2hpbGRfYnVzLnYxLkNoaWxkUghjaGlsZHJlbhI2CgZwaG90b3MYAiADKAsyHi53aGVy'
'ZV9jaGlsZF9idXMudjEuQ2hpbGRQaG90b1IGcGhvdG9z');
'Ch9HZXRDaGlsZExpc3RCeU51cnNlcnlJRFJlc3BvbnNlEi8KBWNoaWxkGAEgASgLMhkud2hlcm'
'VfY2hpbGRfYnVzLnYxLkNoaWxkUgVjaGlsZBI0CgVwaG90bxgCIAEoCzIeLndoZXJlX2NoaWxk'
'X2J1cy52MS5DaGlsZFBob3RvUgVwaG90bw==');

@$core.Deprecated('Use getChildListByGuardianIDRequestDescriptor instead')
const GetChildListByGuardianIDRequest$json = {
Expand Down
Loading
Loading