Skip to content

Commit

Permalink
Merge pull request #106 from GreenTeaProgrammers/revert-105-feature/b…
Browse files Browse the repository at this point in the history
…ackend/api

Revert "Feature/backend/api" Stream Test
  • Loading branch information
KinjiKawaguchi authored Feb 18, 2024
2 parents 286a921 + d4f9eb4 commit 8d105df
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 230 deletions.
2 changes: 2 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ 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,11 +11,6 @@ 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 @@ -34,3 +29,8 @@ 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: 28 additions & 56 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: 21 additions & 19 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(req *pb.GetChildListByNurseryIDRequest, stream pb.ChildService_GetChildListByNurseryIDServer) error {
func (i *Interactor) GetChildListByNurseryID(ctx context.Context, req *pb.GetChildListByNurseryIDRequest) (*pb.GetChildListByNurseryIDResponse, error) {
nurseryID, err := uuid.Parse(req.NurseryId)
if err != nil {
i.logger.Error("failed to parse nursery ID", "error", err)
return err
return nil, err
}

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

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

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

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

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

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
}
// 結果リストに追加
photos = append(photos, &pb.ChildPhoto{
ChildId: child.Id,
Id: photoMetadata.ID.String(), // 修正: GCSから取得した写真のIDではなく、メタデータのIDを使用
PhotoData: photo_data,
})
}
}
return nil

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

func (i *Interactor) GetChildListByBusID(ctx context.Context, req *pb.GetChildListByBusIDRequest) (*pb.GetChildListByBusIDResponse, error) {
Expand Down Expand Up @@ -359,10 +361,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: 0 additions & 10 deletions backend/usecases/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,6 @@ 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({
$8.Child? child,
$8.ChildPhoto? photo,
$core.Iterable<$8.Child>? children,
$core.Iterable<$8.ChildPhoto>? photos,
}) {
final $result = create();
if (child != null) {
$result.child = child;
if (children != null) {
$result.children.addAll(children);
}
if (photo != null) {
$result.photo = photo;
if (photos != null) {
$result.photos.addAll(photos);
}
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)
..aOM<$8.Child>(1, _omitFieldNames ? '' : 'child', subBuilder: $8.Child.create)
..aOM<$8.ChildPhoto>(2, _omitFieldNames ? '' : 'photo', subBuilder: $8.ChildPhoto.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)
..hasRequiredFields = false
;

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

@$pb.TagNumber(1)
$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);
$core.List<$8.Child> get children => $_getList(0);

@$pb.TagNumber(2)
$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);
$core.List<$8.ChildPhoto> get photos => $_getList(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.ResponseStream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($1.GetChildListByNurseryIDRequest request, {$grpc.CallOptions? options}) {
return $createStreamingCall(_$getChildListByNurseryID, $async.Stream.fromIterable([request]), options: options);
$grpc.ResponseFuture<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($1.GetChildListByNurseryIDRequest request, {$grpc.CallOptions? options}) {
return $createUnaryCall(_$getChildListByNurseryID, 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,
true,
false,
($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.Stream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID_Pre($grpc.ServiceCall call, $async.Future<$1.GetChildListByNurseryIDRequest> request) async* {
yield* getChildListByNurseryID(call, await request);
$async.Future<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID_Pre($grpc.ServiceCall call, $async.Future<$1.GetChildListByNurseryIDRequest> request) async {
return 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.Stream<$1.GetChildListByNurseryIDResponse> getChildListByNurseryID($grpc.ServiceCall call, $1.GetChildListByNurseryIDRequest request);
$async.Future<$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': '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'},
{'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'},
],
};

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

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

0 comments on commit 8d105df

Please sign in to comment.