-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from GreenTeaProgrammers/feature/backend/api
feat:apiを実装
- Loading branch information
Showing
13 changed files
with
503 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DSN= | ||
DB_USER_NAME= | ||
DB_PASSWORD= | ||
DB_ADDR= | ||
DB_NAME= | ||
PORT= | ||
MODE_DEV= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/bus" | ||
) | ||
|
||
type busServiceServer struct { | ||
interactor *bus.Interactor | ||
} | ||
|
||
func NewBusServiceServer(interactor *bus.Interactor) pb.BusServiceServer { | ||
return &busServiceServer{interactor} | ||
} | ||
|
||
// GetBusListByNurseryId implements where_child_busv1.BusServiceServer. | ||
func (s *busServiceServer) GetBusListByNurseryId(ctx context.Context, req *pb.GetBusListByNurseryIdRequest) (*pb.GetBusListByNurseryIdResponse, error) { | ||
return s.interactor.GetBusListByNurseryID(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/child" | ||
) | ||
|
||
type childServiceServer struct { | ||
interactor *child.Interactor | ||
} | ||
|
||
func NewChildServiceServer(interactor *child.Interactor) pb.ChildServiceServer { | ||
return &childServiceServer{interactor} | ||
} | ||
|
||
// GetChildListByGuardianID implements where_child_busv1.ChildServiceServer. | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/guardian" | ||
) | ||
|
||
type guardianServiceServer struct { | ||
interactor *guardian.Interactor | ||
} | ||
|
||
func NewGuardianServiceServer(interactor *guardian.Interactor) pb.GuardianServiceServer { | ||
return &guardianServiceServer{interactor} | ||
} | ||
|
||
// GuardianLogin implements where_child_busv1.GuardianServiceServer. | ||
func (s *guardianServiceServer) GuardianLogin(ctx context.Context, req *pb.GuardianLoginRequest) (*pb.GuardianLoginResponse, error) { | ||
return s.interactor.GuardianLogin(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package interfaces | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/nursery" | ||
) | ||
|
||
type nurseryServiceServer struct { | ||
interactor *nursery.Interactor | ||
} | ||
|
||
func NewNurseryServiceServer(interactor *nursery.Interactor) pb.NurseryServiceServer { | ||
return &nurseryServiceServer{interactor} | ||
} | ||
|
||
func (s *nurseryServiceServer) NurseryLogin(ctx context.Context, req *pb.NurseryLoginRequest) (*pb.NurseryLoginResponse, error) { | ||
return s.interactor.NurseryLogin(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package bus | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"golang.org/x/exp/slog" | ||
|
||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/utils" | ||
|
||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent" | ||
busRepo "github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent/bus" | ||
nurseryRepo "github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent/nursery" | ||
) | ||
|
||
type Interactor struct { | ||
entClient *ent.Client | ||
logger *slog.Logger | ||
} | ||
|
||
func NewInteractor(entClient *ent.Client, logger *slog.Logger) *Interactor { | ||
return &Interactor{entClient, logger} | ||
} | ||
|
||
func (i *Interactor) GetBusListByNurseryID(ctx context.Context, req *pb.GetBusListByNurseryIdRequest) (*pb.GetBusListByNurseryIdResponse, error) { | ||
nurseryID, err := uuid.Parse(req.NurseryId) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse nursery ID '%s': %w", req.NurseryId, err) | ||
} | ||
|
||
buses, err := i.getBusList(ctx, func(tx *ent.Tx) (*ent.BusQuery, error) { | ||
return tx.Bus.Query().Where(busRepo.HasNurseryWith(nurseryRepo.IDEQ(nurseryID))), nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pb.GetBusListByNurseryIdResponse{Buses: buses}, nil | ||
} | ||
|
||
func (i *Interactor) getBusList(ctx context.Context, queryFunc func(*ent.Tx) (*ent.BusQuery, error)) ([]*pb.Bus, error) { | ||
tx, err := i.entClient.Tx(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to start transaction: %w", err) | ||
} | ||
defer tx.Rollback() | ||
|
||
query, err := queryFunc(tx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create query: %w", err) | ||
} | ||
|
||
entBuses, err := query.All(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute query: %w", err) | ||
} | ||
|
||
pbBuses := make([]*pb.Bus, len(entBuses)) | ||
for i, b := range entBuses { | ||
pbBuses[i] = utils.ToPbBus(b) | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return nil, fmt.Errorf("failed to commit transaction: %w", err) | ||
} | ||
|
||
return pbBuses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package child | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"golang.org/x/exp/slog" | ||
|
||
"context" | ||
|
||
pb "github.com/GreenTeaProgrammers/WhereChildBus/backend/proto-gen/go/where_child_bus/v1" | ||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/usecases/utils" | ||
|
||
"github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent" | ||
childRepo "github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent/child" | ||
guardianRepo "github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent/guardian" | ||
nurseryRepo "github.com/GreenTeaProgrammers/WhereChildBus/backend/domain/repository/ent/nursery" | ||
) | ||
|
||
type Interactor struct { | ||
entClient *ent.Client | ||
logger *slog.Logger | ||
} | ||
|
||
func NewInteractor(entClient *ent.Client, logger *slog.Logger) *Interactor { | ||
return &Interactor{entClient, logger} | ||
} | ||
|
||
func (i *Interactor) GetChildListByGuardianID(ctx context.Context, req *pb.GetChildListByGuardianIDRequest) (*pb.GetChildListByGuardianIDResponse, error) { | ||
guardianID, err := uuid.Parse(req.GuardianId) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse guardian ID '%s': %w", req.GuardianId, err) | ||
} | ||
|
||
children, err := i.getChildList(ctx, func(tx *ent.Tx) (*ent.ChildQuery, error) { | ||
return tx.Child.Query().Where(childRepo.HasGuardianWith(guardianRepo.IDEQ(guardianID))), nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pb.GetChildListByGuardianIDResponse{Children: children}, nil | ||
} | ||
|
||
func (i *Interactor) GetChildListByNurseryID(ctx context.Context, req *pb.GetChildListByNurseryIDRequest) (*pb.GetChildListByNurseryIDResponse, error) { | ||
nurseryID, err := uuid.Parse(req.NurseryId) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse nursery ID '%s': %w", req.NurseryId, err) | ||
} | ||
|
||
children, err := i.getChildList(ctx, func(tx *ent.Tx) (*ent.ChildQuery, error) { | ||
return tx.Child.Query().Where(childRepo.HasNurseryWith(nurseryRepo.IDEQ(nurseryID))), nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &pb.GetChildListByNurseryIDResponse{Children: children}, nil | ||
} | ||
|
||
// getChildList abstracts the common logic for fetching child lists. | ||
func (i *Interactor) getChildList(ctx context.Context, queryFunc func(*ent.Tx) (*ent.ChildQuery, error)) ([]*pb.Child, error) { | ||
tx, err := i.entClient.Tx(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to start transaction: %w", err) | ||
} | ||
defer tx.Rollback() // Rollback is safe to call even if the tx is already committed. | ||
|
||
query, err := queryFunc(tx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entChildren, err := query.All(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("query failed: %w", err) | ||
} | ||
|
||
pbChildren := make([]*pb.Child, len(entChildren)) | ||
for i, c := range entChildren { | ||
pbChildren[i] = utils.ToPbChild(c) | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return nil, fmt.Errorf("failed to commit transaction: %w", err) | ||
} | ||
|
||
return pbChildren, nil | ||
} |
Oops, something went wrong.