From 44d831785e8bef6167dde3f965f480172d1f42dc Mon Sep 17 00:00:00 2001
From: Hueter <kohsukemori@gmail.com>
Date: Wed, 2 Oct 2024 01:28:19 +0900
Subject: [PATCH] =?UTF-8?q?for=E6=96=87=E3=82=92samber/lo=E3=81=A7?=
 =?UTF-8?q?=E7=BD=AE=E3=81=8D=E6=8F=9B=E3=81=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 router/group.go           |  7 +++----
 router/request.go         |  9 ++++-----
 service/webhook.go        | 15 +++++++--------
 testutil/random/ramdom.go | 26 +++++++++++---------------
 4 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/router/group.go b/router/group.go
index 4a719d3d..5022f35d 100644
--- a/router/group.go
+++ b/router/group.go
@@ -299,10 +299,9 @@ func (h Handlers) PostOwner(c echo.Context) error {
 		return echo.NewHTTPError(http.StatusInternalServerError, err)
 	}
 
-	res := make([]uuid.UUID, len(added))
-	for i, owner := range added {
-		res[i] = owner.ID
-	}
+	res := lo.Map(added, func(owner *model.Owner, index int) uuid.UUID {
+		return owner.ID
+	})
 
 	return c.JSON(http.StatusOK, res)
 }
diff --git a/router/request.go b/router/request.go
index 0777862a..89d77c93 100644
--- a/router/request.go
+++ b/router/request.go
@@ -715,13 +715,12 @@ func (h Handlers) PutStatus(c echo.Context) error {
 				h.Logger.Error("failed to get request targets from repository", zap.Error(err))
 				return echo.NewHTTPError(http.StatusInternalServerError, err)
 			}
-			var paid bool
-			for _, target := range targets {
+			paid := lo.Reduce(targets, func(p bool, target *model.RequestTargetDetail, _ int) bool {
 				if target.PaidAt != nil {
-					paid = true
-					break
+					return true
 				}
-			}
+				return p
+			}, false)
 			if paid {
 				h.Logger.Info("someone already paid")
 				return echo.NewHTTPError(http.StatusBadRequest, errors.New("someone already paid"))
diff --git a/service/webhook.go b/service/webhook.go
index d1f8664f..d7801670 100644
--- a/service/webhook.go
+++ b/service/webhook.go
@@ -117,10 +117,9 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) {
 				"https://jomon.trap.jp",
 				resApp.ID)
 
-			amount := 0
-			for _, target := range resApp.Targets {
-				amount += target.Amount
-			}
+			amount := lo.Reduce(resApp.Targets, func(amo int, target *Target, _ int) int {
+				return amo + target.Amount
+			}, 0)
 			message += fmt.Sprintf("- 支払金額: %d円\n", amount)
 
 			if resApp.Group != nil {
@@ -167,10 +166,10 @@ func WebhookEventHandler(c echo.Context, reqBody, resBody []byte) {
 					resApp.Amount)
 			}
 		} else {
-			targets := make([]string, len(resApps))
-			for i, resApp := range resApps {
-				targets[i] = resApp.Target
-			}
+			targets := lo.Map(
+				resApps, func(resApp TransactionRequestApplication, index int) string {
+					return resApp.Target
+				})
 			if resApp.Amount < 0 {
 				message += fmt.Sprintf(
 					"- %sへの支払い\n    - 支払い金額: 計%d円(一人当たりへの支払い金額: %d円)\n",
diff --git a/testutil/random/ramdom.go b/testutil/random/ramdom.go
index 1b3645fc..b6dbd3c4 100644
--- a/testutil/random/ramdom.go
+++ b/testutil/random/ramdom.go
@@ -3,6 +3,8 @@ package random
 import (
 	"math/rand/v2"
 	"testing"
+
+	"github.com/samber/lo"
 )
 
 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
@@ -32,27 +34,21 @@ func Numeric64(t *testing.T, n int64) int64 {
 
 func AlphaNumericSlice(t *testing.T, length int, n int64) []string {
 	t.Helper()
-	slice := []string{}
-	for range length {
-		slice = append(slice, AlphaNumeric(t, int(n)))
-	}
-	return slice
+	return lo.Times(length, func(index int) string {
+		return AlphaNumeric(t, int(n))
+	})
 }
 
 func NumericSlice(t *testing.T, length int, n int) []int {
 	t.Helper()
-	slice := []int{}
-	for range length {
-		slice = append(slice, Numeric(t, n))
-	}
-	return slice
+	return lo.Times(length, func(index int) int {
+		return Numeric(t, n)
+	})
 }
 
 func Numeric64Slice(t *testing.T, length int, n int64) []int64 {
 	t.Helper()
-	slice := []int64{}
-	for range length {
-		slice = append(slice, Numeric64(t, n))
-	}
-	return slice
+	return lo.Times(length, func(index int) int64 {
+		return Numeric64(t, n)
+	})
 }