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

/items?rental=hoge が遅い #520

Open
ryoha000 opened this issue May 11, 2022 · 1 comment
Open

/items?rental=hoge が遅い #520

ryoha000 opened this issue May 11, 2022 · 1 comment
Assignees

Comments

@ryoha000
Copy link
Member

indexのせい?要調査

@H1rono H1rono self-assigned this May 2, 2023
@H1rono
Copy link
Member

H1rono commented May 5, 2023

該当部分

booQ/router/items.go

Lines 42 to 56 in c56f2b4

rentalName := c.QueryParam("rental")
if rentalName != "" {
user, err := model.GetUserByName(rentalName)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
if user.ID == 0 {
return c.JSON(http.StatusBadRequest, errors.New("指定してるNameが不正です"))
}
res, err := model.SearchItemByRental(user.ID, me.ID)
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
return c.JSON(http.StatusOK, res)
}

booQ/model/users.go

Lines 45 to 56 in c56f2b4

// GetUserByName userをNameから取得する
func GetUserByName(name string) (User, error) {
res := User{}
err := db.Where("name = ?", name).First(&res).Error
if err != nil {
if gorm.IsRecordNotFoundError(err) {
return User{}, errors.New("nameが存在しません")
}
return User{}, err
}
return res, nil
}

booQ/model/items.go

Lines 428 to 465 in c56f2b4

// SearchItemsByRental itemsをRentalUserNameから取得する
func SearchItemByRental(rentalUserID uint, meID uint) ([]GetItemResponse, error) {
items := []Item{}
err := db.Set("gorm:auto_preload", true).Preload("Logs.User").Preload("RentalUsers.User").Preload("RentalUsers.Owner").Preload("Comments.User").Find(&items).Error
if err != nil {
return []GetItemResponse{}, err
}
res := make([]GetItemResponse, 0, len(items))
for _, item := range items {
var err error
item.LatestLogs, err = GetLatestLogs(item.Logs)
if err != nil {
return []GetItemResponse{}, err
}
match := false
for _, rentalUser := range item.RentalUsers {
if rentalUser.UserID == rentalUserID && rentalUser.Count < 0 {
match = true
}
}
if !match {
continue
}
isLiked := false
for _, like := range item.Likes {
if like.ID == meID {
isLiked = true
break
}
}
r := GetItemResponse{
IsLiked: isLiked,
Item: item,
}
res = append(res, r)
}
return res, nil
}

SearchItemByRentalをいい感じにできそう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants