How to use Page method of result Package

Best Testkube code snippet using result.Page

datastore.go

Source:datastore.go Github

copy

Full Screen

1package datastore2import (3 "fmt"4 _ "os"5 "strconv"6 "strings"7 "github.com/mak-alex/mopds/models"8 "github.com/mak-alex/mopds/modules/paginate"9 "github.com/mak-alex/mopds/utils"10 "github.com/jinzhu/gorm"11 _ "github.com/jinzhu/gorm/dialects/mysql"12 _ "github.com/jinzhu/gorm/dialects/postgres"13 _ "github.com/jinzhu/gorm/dialects/sqlite"14)15// TODO: вынести повторяющееся в отдельные методы и т.д.16const (17 DEFAULT_PAGE_SIZE int = 2018 MAX_PAGE_SIZE int = 100019 CAT_NORMAL int = 020 CAT_ZIP int = 121 CAT_INPX int = 222 CAT_INP int = 323)24type dbStore struct {25 db *gorm.DB26}27func addParams(search *gorm.DB, params models.Search) *gorm.DB {28 if !params.Deleted {29 search = search.Where("del='0'")30 }31 if len(params.Langs) > 0 {32 for i, _ := range params.Langs {33 params.Langs[i] = "'" + params.Langs[i] + "'"34 }35 search = search.Where("lang in (" + strings.Join(params.Langs, ",") + ")")36 }37 return search38}39func (store *dbStore) PutBook(book *models.Book) (err error) {40 tx := store.db.Begin()41 book.Catalog.CatType = 042 store.db.FirstOrCreate(&book.Catalog, book.Catalog)43 authors := []models.Author{}44 for _, author := range book.Authors {45 filledAuthor := models.Author{}46 store.db.FirstOrCreate(&filledAuthor, author)47 authors = append(authors, filledAuthor)48 }49 book.Authors = authors50 genres := []models.Genre{}51 for _, genre := range book.Genres {52 filledGenre := models.Genre{}53 store.db.FirstOrCreate(&filledGenre, genre)54 genres = append(genres, filledGenre)55 }56 book.Genres = genres57 covers := []models.Cover{}58 for _, cover := range book.Covers {59 filledCover := models.Cover{}60 store.db.FirstOrCreate(&filledCover, cover)61 covers = append(covers, filledCover)62 }63 book.Covers = covers64 /*annotations := []models.Annotation{}65 for _, annotation := range book.Annotations {66 filledAnnotation := models.Annotation{}67 store.db.FirstOrCreate(&filledAnnotation, annotation)68 annotations = append(annotations, filledAnnotation)69 }70 book.Annotations = annotations*/71 store.db.Create(&book)72 tx.Commit()73 return err74}75func (store *dbStore) fillBookDetails(book *models.Book, fillGenres bool, fillCovers bool, fillCatalogs bool, fillAnnotations bool) {76 store.db.Select("authors.*").Model(book).Related(&book.Authors, "Authors")77 for j, a := range book.Authors {78 book.Authors[j].FullName = utils.UpperInitialAll(a.FullName)79 }80 if fillGenres {81 store.db.Select("genres.*").Model(book).Related(&book.Genres, "Genres")82 }83 if fillCovers {84 store.db.Select("covers.*").Model(book).Related(&book.Covers, "Covers")85 }86 if fillCatalogs {87 store.db.Select("catalogs.*").Model(book).Related(&book.Catalog, "Catalog")88 }89 if fillAnnotations {90 store.db.Select("annotations.*").Model(book).Related(&book.Annotations, "Annotations")91 }92}93func (store *dbStore) fillBooksDetails(books []models.Book, fillGenres bool, fillCovers bool, fillCatalogs bool, fillAnnotations bool) []models.Book {94 for i, _ := range books {95 store.fillBookDetails(&books[i], fillGenres, fillCovers, fillCatalogs, fillAnnotations)96 }97 return books98}99func (store *dbStore) GetRandomBooksByAuthorID(authorID uint, noDetails bool, page int, per_page int) (*paginate.PaginatedList, error) {100 count := 0101 result := []models.Book{}102 search := store.db.Select("books.*").Table("books").103 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bgenres on bgenres.book_id = books.id left join genres on bgenres.genre_id=genres.id")104 search = search.Where("authors.ID=?", authorID)105 //search = addParams(search, params).Group("books.id")106 if per_page <= 0 {107 per_page = DEFAULT_PAGE_SIZE108 }109 if per_page > MAX_PAGE_SIZE {110 per_page = MAX_PAGE_SIZE111 }112 if page == 0 {113 page = 1114 }115 search.Count(&count)116 p := paginate.NewPaginatedList(page, per_page, count)117 search = search.Limit(p.Limit())118 search = search.Offset(p.Offset())119 search.Preload("Catalog").Order("RANDOM()").Find(&result)120 //p.Items = result121 if !noDetails {122 p.Items = store.fillBooksDetails(result, true, true, true, true)123 } else {124 p.Items = store.fillBooksDetails(result, false, false, false, false)125 }126 return p, nil127}128func (store *dbStore) GetRandomBooksByGenreID(genreID uint, noDetails bool, page int, per_page int) (*paginate.PaginatedList, error) {129 count := 0130 result := []models.Book{}131 search := store.db.Select("books.*").Table("books").132 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bgenres on bgenres.book_id = books.id left join genres on bgenres.genre_id=genres.id")133 search = search.Where("genres.ID=?", genreID)134 //search = addParams(search, params).Group("books.id")135 if per_page <= 0 {136 per_page = DEFAULT_PAGE_SIZE137 }138 if per_page > MAX_PAGE_SIZE {139 per_page = MAX_PAGE_SIZE140 }141 if page == 0 {142 page = 1143 }144 search.Count(&count)145 p := paginate.NewPaginatedList(page, per_page, count)146 search = search.Limit(p.Limit())147 search = search.Offset(p.Offset())148 search.Preload("Catalog").Order("RANDOM()").Find(&result)149 //p.Items = result150 if !noDetails {151 p.Items = store.fillBooksDetails(result, true, true, true, true)152 } else {153 p.Items = store.fillBooksDetails(result, false, false, false, false)154 }155 return p, nil156}157func (store *dbStore) GetRandomBooksBySerieID(serieID uint, noDetails bool, page int, per_page int) (*paginate.PaginatedList, error) {158 count := 0159 result := []models.Book{}160 search := store.db.Select("books.*").Table("books").161 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bseries on bseries.book_id = books.id left join series on bseries.serie_id=series.id")162 search = search.Where("series.ID=?", serieID)163 //search = addParams(search, params).Group("books.id")164 if per_page <= 0 {165 per_page = DEFAULT_PAGE_SIZE166 }167 if per_page > MAX_PAGE_SIZE {168 per_page = MAX_PAGE_SIZE169 }170 if page == 0 {171 page = 1172 }173 search.Count(&count)174 p := paginate.NewPaginatedList(page, per_page, count)175 search = search.Limit(p.Limit())176 search = search.Offset(p.Offset())177 search.Preload("Catalog").Order("RANDOM()").Find(&result)178 //p.Items = result179 if !noDetails {180 p.Items = store.fillBooksDetails(result, true, true, true, true)181 } else {182 p.Items = store.fillBooksDetails(result, false, false, false, false)183 }184 return p, nil185}186func (store *dbStore) GetBooksBySerie(title string, series string, noDetails bool, page int, per_page int) (*paginate.PaginatedList, error) {187 count := 0188 result := []models.Book{}189 search := store.db.Select("books.*").Table("books").190 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bseries on bseries.book_id = books.id left join series on bseries.serie_id=series.id")191 for _, term := range utils.SplitBySeparators(strings.ToLower(title)) {192 search = search.Where("LOWER(title) LIKE ?", "%"+term+"%")193 }194 for _, term := range utils.SplitBySeparators(strings.ToLower(series)) {195 search = search.Where("LOWER(ser) LIKE ?", "%"+term+"%")196 }197 //search = addParams(search, params).Group("books.id")198 if per_page <= 0 {199 per_page = DEFAULT_PAGE_SIZE200 }201 if per_page > MAX_PAGE_SIZE {202 per_page = MAX_PAGE_SIZE203 }204 if page == 0 {205 page = 1206 }207 search.Count(&count)208 p := paginate.NewPaginatedList(page, per_page, count)209 search = search.Limit(p.Limit())210 search = search.Offset(p.Offset())211 search.Preload("Catalog").Order("ser_no, title").Find(&result)212 //p.Items = result213 if !noDetails {214 p.Items = store.fillBooksDetails(result, true, true, true, true)215 } else {216 p.Items = store.fillBooksDetails(result, false, false, false, false)217 }218 return p, nil219}220func (store *dbStore) GetBooksByLibID(libID string, noDetails bool, page int, per_page int) (*paginate.PaginatedList, error) {221 count := 0222 result := []models.Book{}223 search := store.db.Select("books.*").Table("books")224 search = search.Where("lib_id = ?", libID)225 if per_page <= 0 {226 per_page = DEFAULT_PAGE_SIZE227 }228 if per_page > MAX_PAGE_SIZE {229 per_page = MAX_PAGE_SIZE230 }231 if page == 0 {232 page = 1233 }234 search.Count(&count)235 p := paginate.NewPaginatedList(page, per_page, count)236 search = search.Limit(p.Limit())237 search = search.Offset(p.Offset())238 search.Find(&result)239 if !noDetails {240 p.Items = store.fillBooksDetails(result, true, true, true, true)241 } else {242 p.Items = store.fillBooksDetails(result, false, false, false, false)243 }244 return p, nil245}246func (store *dbStore) GetAuthors(author string, page int, per_page int) (*paginate.PaginatedList, error) {247 count := 0248 result := []models.Author{}249 search := store.db.Select("authors.*").Table("authors")250 for _, term := range utils.SplitBySeparators(strings.ToLower(author)) {251 search = search.Where("LOWER(full_name) LIKE ?", "%"+term+"%")252 }253 if per_page <= 0 {254 per_page = DEFAULT_PAGE_SIZE255 }256 if per_page > MAX_PAGE_SIZE {257 per_page = MAX_PAGE_SIZE258 }259 if page == 0 {260 page = 1261 }262 search.Count(&count)263 p := paginate.NewPaginatedList(page, per_page, count)264 search = search.Order("full_name")265 search = search.Limit(p.Limit())266 search = search.Offset(p.Offset())267 search.Find(&result)268 p.Items = result269 return p, nil270}271func (store *dbStore) GetCover(coverID uint) (*models.Cover, error) {272 result := new(models.Cover)273 store.db.First(result, coverID)274 if result.ID > 0 {275 //result.FullName = utils.UpperInitialAll(result.FullName)276 return result, nil277 }278 return nil, fmt.Errorf("No cover found")279}280func (store *dbStore) GetGenre(genreID uint) (*models.Genre, error) {281 result := new(models.Genre)282 store.db.First(result, genreID)283 if result.ID > 0 {284 result.Genre = utils.UpperInitialAll(result.Genre)285 return result, nil286 }287 return nil, fmt.Errorf("No genre found")288}289func (store *dbStore) GetSerie(serieID uint) (*models.Serie, error) {290 result := new(models.Serie)291 store.db.First(result, serieID)292 if result.ID > 0 {293 result.Ser = utils.UpperInitialAll(result.Ser)294 return result, nil295 }296 return nil, fmt.Errorf("No serie found")297}298func (store *dbStore) GetAuthor(authorID uint) (*models.Author, error) {299 result := new(models.Author)300 store.db.First(result, authorID)301 if result.ID > 0 {302 result.FullName = utils.UpperInitialAll(result.FullName)303 return result, nil304 }305 return nil, fmt.Errorf("No author found")306}307func (store *dbStore) GetSeries(serie string, page int, per_page int) (*paginate.PaginatedList, error) {308 count := 0309 result := []models.Serie{}310 search := store.db.Select("series*").Table("series")311 for _, term := range utils.SplitBySeparators(strings.ToLower(serie)) {312 search = search.Where("LOWER(ser) LIKE ?", "%"+term+"%")313 }314 if per_page <= 0 {315 per_page = DEFAULT_PAGE_SIZE316 }317 if per_page > MAX_PAGE_SIZE {318 per_page = MAX_PAGE_SIZE319 }320 if page == 0 {321 page = 1322 }323 search.Count(&count)324 p := paginate.NewPaginatedList(page, per_page, count)325 search = search.Limit(p.Limit())326 search = search.Offset(p.Offset())327 search.Select("series.*").Table("series").Find(&result)328 p.Items = result329 return p, nil330}331func (store *dbStore) GetSummary() (models.Summary, error) {332 summary := models.Summary{}333 authors_count := 0334 books_count := 0335 genres_count := 0336 series_count := 0337 catalogs_count := 0338 store.db.Table("authors").Count(&authors_count)339 store.db.Table("books").Count(&books_count)340 store.db.Table("genres").Count(&genres_count)341 store.db.Table("series").Count(&series_count)342 store.db.Table("catalogs").Count(&catalogs_count)343 summary.Authors = authors_count344 summary.Books = books_count345 summary.Genres = genres_count346 summary.Series = series_count347 summary.Catalogs = catalogs_count348 return summary, nil349}350func (store *dbStore) GetGenresMenu() ([]models.ItemMenu, error) {351 item := []models.ItemMenu{}352 found := make(map[string]bool)353 result := []models.Genre{}354 store.db.Select("genres.*").Table("genres").Find(&result)355 // create first level menu356 for i, genre := range result {357 i++358 if !found[genre.Section] {359 item = append(item, models.ItemMenu{ID: strconv.Itoa(i), Value: genre.Section})360 found[genre.Section] = true361 }362 }363 // create sublevel menu364 for j, genre := range result {365 j++366 for i, _ := range item {367 if genre.Section == item[i].Value {368 item[i].Data = append(item[i].Data, models.SubItemMenu{ID: fmt.Sprintf("%s.%s", strconv.Itoa(j), strconv.Itoa(i)), GenreID: genre.ID, Value: genre.Subsection})369 }370 }371 }372 return item, nil373}374func (store *dbStore) GetGenres(genre string, page int, per_page int) (*paginate.PaginatedList, error) {375 count := 0376 result := []models.Genre{}377 search := store.db.Select("genres.*").Table("genres")378 for _, term := range utils.SplitBySeparators(strings.ToLower(genre)) {379 search = search.Where("LOWER(genre) LIKE ? OR LOWER(section) LIKE ? OR LOWER(subsection) LIKE ?", "%"+term+"%", "%"+term+"%", "%"+term+"%")380 }381 if per_page <= 0 {382 per_page = DEFAULT_PAGE_SIZE383 }384 if per_page > MAX_PAGE_SIZE {385 per_page = MAX_PAGE_SIZE386 }387 if page == 0 {388 page = 1389 }390 search.Count(&count)391 p := paginate.NewPaginatedList(page, per_page, count)392 search = search.Limit(p.Limit())393 search = search.Offset(p.Offset())394 search.Select("genres.*").Table("genres").Find(&result)395 p.Items = result396 return p, nil397}398func (store *dbStore) GetBooks(title string, noDetails bool, random bool, page int, per_page int) (*paginate.PaginatedList, error) {399 count := 0400 result := []models.Book{}401 search := store.db.Select("books.*").Table("books").402 // select * from books left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id403 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bseries on bseries.book_id = books.id left join series on bseries.serie_id=series.id")404 for _, term := range utils.SplitBySeparators(strings.ToLower(title)) {405 search = search.Where("LOWER(title) LIKE ?", "%"+term+"%")406 }407 if per_page <= 0 {408 per_page = DEFAULT_PAGE_SIZE409 }410 if per_page > MAX_PAGE_SIZE {411 per_page = MAX_PAGE_SIZE412 }413 if page == 0 {414 page = 1415 }416 for _, term := range utils.SplitBySeparators(strings.ToLower(title)) {417 search = search.Where("LOWER(title) LIKE ?", "%"+term+"%")418 }419 search.Count(&count)420 p := paginate.NewPaginatedList(page, per_page, count)421 search = search.Limit(p.Limit())422 search = search.Offset(p.Offset())423 if random {424 search.Group("books.id").Select("books.*").Table("books").Order("RANDOM()").Find(&result)425 } else {426 search.Group("books.id").Select("books.*").Table("books").Find(&result)427 }428 // utils.PrintJson(result, true)429 if !noDetails {430 p.Items = store.fillBooksDetails(result, true, true, true, true)431 } else {432 p.Items = store.fillBooksDetails(result, false, false, false, false)433 }434 return p, nil435}436func (store *dbStore) GetBooksByGenreID(genreID uint, noDetails bool, random bool, page int, per_page int, params models.Search) (*paginate.PaginatedList, error) {437 count := 0438 result := []models.Book{}439 search := store.db.Select("books.*").Table("books").440 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bgenres on bgenres.book_id = books.id left join genres on bgenres.genre_id=genres.id")441 search = search.Where("genres.ID=?", genreID)442 search.Count(&count)443 search = addParams(search, params).Group("books.id")444 if per_page <= 0 {445 per_page = DEFAULT_PAGE_SIZE446 }447 if per_page >= count {448 per_page = count449 }450 if per_page > MAX_PAGE_SIZE {451 per_page = MAX_PAGE_SIZE452 }453 if page == 0 {454 page = 1455 }456 p := paginate.NewPaginatedList(page, per_page, count)457 search = search.Limit(p.Limit())458 search = search.Offset(p.Offset())459 if random {460 search.Preload("Catalog").Order("RANDOM()").Find(&result)461 } else {462 search.Preload("Catalog").Find(&result)463 }464 if !noDetails {465 //p.Items = store.fillBooksDetails(result, false, false, false, false)466 p.Items = store.fillBooksDetails(result, true, true, true, true)467 } else {468 p.Items = store.fillBooksDetails(result, false, false, false, false)469 }470 p.Items = result471 return p, nil472}473func (store *dbStore) GetBooksBySerieID(serieID uint, noDetails bool, random bool, page int, per_page int, params models.Search) (*paginate.PaginatedList, error) {474 count := 0475 result := []models.Book{}476 search := store.db.Select("books.*").Table("books").477 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bseries on bseries.book_id = books.id left join series on bseries.serie_id=series.id")478 search = search.Where("series.ID=?", serieID)479 search.Count(&count)480 search = addParams(search, params).Group("books.id")481 if per_page <= 0 {482 per_page = DEFAULT_PAGE_SIZE483 }484 if per_page >= count {485 per_page = count486 }487 if per_page > MAX_PAGE_SIZE {488 per_page = MAX_PAGE_SIZE489 }490 if page == 0 {491 page = 1492 }493 p := paginate.NewPaginatedList(page, per_page, count)494 search = search.Limit(p.Limit())495 search = search.Offset(p.Offset())496 if random {497 search.Preload("Catalog").Order("RANDOM()").Find(&result)498 } else {499 search.Preload("Catalog").Find(&result)500 }501 if !noDetails {502 //p.Items = store.fillBooksDetails(result, false, false, false, false)503 p.Items = store.fillBooksDetails(result, true, true, true, true)504 } else {505 p.Items = store.fillBooksDetails(result, false, false, false, false)506 }507 p.Items = result508 return p, nil509}510func (store *dbStore) GetBooksByAuthorID(authorID uint, noDetails bool, random bool, page int, per_page int, params models.Search) (*paginate.PaginatedList, error) {511 count := 0512 result := []models.Book{}513 search := store.db.Select("books.*").Table("books").514 Joins("left join bauthors on books.id=bauthors.book_id left join authors on authors.id=bauthors.author_id left join bcovers on books.id=bcovers.book_id left join covers on covers.id=bcovers.book_id left join bseries on bseries.book_id = books.id left join series on bseries.serie_id=series.id")515 search = search.Where("authors.ID=?", authorID)516 search.Count(&count)517 search = addParams(search, params).Group("books.id")518 if per_page <= 0 {519 per_page = DEFAULT_PAGE_SIZE520 }521 if per_page >= count {522 per_page = count523 }524 if per_page > MAX_PAGE_SIZE {525 per_page = MAX_PAGE_SIZE526 }527 if page == 0 {528 page = 1529 }530 p := paginate.NewPaginatedList(page, per_page, count)531 search = search.Limit(p.Limit())532 search = search.Offset(p.Offset())533 if random {534 search.Preload("Catalog").Order("RANDOM()").Find(&result)535 } else {536 search.Preload("Catalog").Find(&result)537 }538 if !noDetails {539 //p.Items = store.fillBooksDetails(result, false, false, false, false)540 p.Items = store.fillBooksDetails(result, true, true, true, true)541 } else {542 p.Items = store.fillBooksDetails(result, false, false, false, false)543 }544 p.Items = result545 return p, nil546}547func (store *dbStore) GetBook(bookID uint) (*models.Book, error) {548 result := new(models.Book)549 store.db.Select("*").Preload("Catalog").First(result, bookID)550 store.fillBookDetails(result, true, true, true, true)551 if result.ID > 0 {552 return result, nil553 }554 return nil, fmt.Errorf("No book found")555}556func (store *dbStore) UpdateBook(book *models.Book) (*models.Book, error) {557 found := new(models.Book)558 store.db.Select("distinct books.*").Table("books").559 Joins("left join catalogs on catalogs.id = books.catalog_id").560 Where("lib_id = ? and file_name = ?", book.LibID, book.Catalog.CatName).561 First(found)562 book.ID = found.ID563 book.CatalogID = found.CatalogID564 book.Catalog = models.Catalog{}565 // if found != book {566 // store.db.Save(book)567 // }568 return book, nil569}570func (store *dbStore) GetLangs() ([]string, error) {571 var result []string572 found := []models.Book{}573 store.db.Select("distinct books.lang").574 Table("books").Where("lang <> ''").575 Order("lang").576 Find(&found)577 for _, book := range found {578 result = append(result, book.Lang)579 }580 return result, nil581}582func (store *dbStore) IsFileExist(fileName string) bool {583 contObj := new(models.Book)584 store.db.Where("file_name = ?", fileName).First(&contObj)585 return contObj.ID > 0586}587func (store *dbStore) IsCatalogExist(fileName string) bool {588 contObj := new(models.Catalog)589 store.db.Where("cat_name = ?", fileName).First(&contObj)590 return contObj.ID > 0591}592func (store *dbStore) Close() {593 store.db.Close()594}595// NewDBStore creates new instance of datastorer596func NewDBStore(config *models.DBConfig) (DataStorer, error) {597 db, err := gorm.Open(config.DBType, config.DBParams)598 if err == nil {599 db.DB()600 db.AutoMigrate(&models.Author{}, &models.Book{}, &models.Annotation{}, &models.Cover{}, &models.Catalog{}, &models.Genre{}, &models.Serie{})601 db.LogMode(config.DBLog)602 }603 result := new(dbStore)604 result.db = db605 return result, err606}...

Full Screen

Full Screen

sync.go

Source:sync.go Github

copy

Full Screen

...53// SyncCommit ...54func (s *Service) SyncCommit() (err error) {55 var (56 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs57 allPage int58 allNum int59 result *model.SyncResult60 )61 log.Info("===================== SyncCommit start ========================")62 for _, projectID := range projectIDs {63 if result, err = s.SyncProjectCommit(projectID); err != nil {64 log.Error("SyncCommit projectID(%d), err(%+v)", projectID, err)65 go s.WechatFailData(model.DataTypeCommit, projectID, result, err)66 return67 }68 log.Info(">>>>>>>>> SyncCommit projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)69 if result != nil && len(result.FailData) > 0 {70 go s.WechatFailData(model.DataTypeCommit, projectID, result, nil)71 }72 allPage = allPage + result.TotalPage73 allNum = allNum + result.TotalNum74 }75 log.Info("===================== SyncCommit finished totalPage(%d), totalNum(%d)========================", allPage, allNum)76 return77}78// SyncMR ...79func (s *Service) SyncMR() (err error) {80 var (81 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs82 allPage int83 allNum int84 result *model.SyncResult85 )86 log.Info("===================== SyncMR start ========================")87 for _, projectID := range projectIDs {88 if result, err = s.SyncProjectMR(context.TODO(), projectID); err != nil {89 log.Error("SyncMR projectID(%d), err(%+v)", projectID, err)90 go s.WechatFailData(model.DataTypeMR, projectID, result, err)91 return92 }93 log.Info(">>>>>>>>> SyncMR projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)94 if result != nil && len(result.FailData) > 0 {95 go s.WechatFailData(model.DataTypeMR, projectID, result, nil)96 }97 allPage = allPage + result.TotalPage98 allNum = allNum + result.TotalNum99 }100 log.Info("===================== SyncMR finished totalPage(%d), totalNum(%d)========================", allPage, allNum)101 return102}103// AggregateMR ...104func (s *Service) AggregateMR() (err error) {105 var (106 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs107 )108 log.Info("===================== AggMR start ========================")109 for _, projectID := range projectIDs {110 if err = s.AggregateProjectMR(context.TODO(), projectID); err != nil {111 log.Error("AggMR projectID(%d), err(%+v)", projectID, err)112 return113 }114 log.Info(">>>>>>>>> AggMR projectID(%d) complete", projectID)115 }116 log.Info("===================== AggMR finished ========================")117 return118}119// SyncJobs ...120func (s *Service) SyncJobs() (err error) {121 var (122 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs123 allPage int124 allNum int125 result *model.SyncResult126 )127 log.Info("===================== SyncJobs start ========================")128 for _, projectID := range projectIDs {129 if result, err = s.SyncProjectJobs(projectID); err != nil {130 log.Error("SyncJobs projectID(%d), err(%+v)", projectID, err)131 go s.WechatFailData(model.DataTypeJob, projectID, result, err)132 return133 }134 log.Info(">>>>>>>>> SyncJobs projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)135 if result != nil && len(result.FailData) > 0 {136 go s.WechatFailData(model.DataTypeJob, projectID, result, nil)137 }138 allPage = allPage + result.TotalPage139 allNum = allNum + result.TotalNum140 }141 log.Info("===================== SyncJobs finished totalPage(%d), totalNum(%d)========================", allPage, allNum)142 return143}144// SyncPipelines ...145func (s *Service) SyncPipelines() (err error) {146 var (147 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs148 allPage int149 allNum int150 result *model.SyncResult151 )152 log.Info("===================== SyncPipelines start ========================")153 for _, projectID := range projectIDs {154 if result, err = s.SyncProjectPipelines(projectID); err != nil {155 log.Error("SyncPipelines projectID(%d), err(%+v)", projectID, err)156 go s.WechatFailData(model.DataTypePipeline, projectID, result, err)157 return158 }159 log.Info(">>>>>>>>> SyncPipelines projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)160 if result != nil && len(result.FailData) > 0 {161 go s.WechatFailData(model.DataTypePipeline, projectID, result, nil)162 }163 allPage = allPage + result.TotalPage164 allNum = allNum + result.TotalNum165 }166 log.Info("===================== SyncPipelines finished totalPage(%d), totalNum(%d)========================", allPage, allNum)167 return168}169// SyncMRNote ...170func (s *Service) SyncMRNote() (err error) {171 var (172 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs173 totalPage int174 totalNum int175 page int176 num int177 )178 log.Info("===================== SyncNote start ========================")179 for _, projectID := range projectIDs {180 if page, num, err = s.SyncProjectNotes(context.TODO(), projectID); err != nil {181 log.Error("SyncNotes projectID(%d), err(%+v)", projectID, err)182 return183 }184 log.Info(">>>>>>>>> SyncNote projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)185 totalPage = totalPage + page186 totalNum = totalNum + num187 }188 log.Info("===================== SyncNote finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)189 return190}191// SyncMember ...192func (s *Service) SyncMember() (err error) {193 var (194 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs195 totalPage int196 totalNum int197 page int198 num int199 )200 log.Info("===================== SyncMember start ========================")201 for _, projectID := range projectIDs {202 if page, num, err = s.SyncProjectMember(context.TODO(), projectID); err != nil {203 log.Error("SyncMember projectID(%d), err(%+v)", projectID, err)204 return205 }206 log.Info(">>>>>>>>> SyncMember projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)207 totalPage = totalPage + page208 totalNum = totalNum + num209 }210 log.Info("===================== SyncMember finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)211 return212}213// SyncMRAwardEmoji ...214func (s *Service) SyncMRAwardEmoji() (err error) {215 var (216 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs217 totalPage int218 totalNum int219 page int220 num int221 )222 log.Info("===================== SyncMRAwardEmoji start ========================")223 for _, projectID := range projectIDs {224 if page, num, err = s.SyncProjectAwardEmoji(context.TODO(), projectID); err != nil {225 log.Error("SyncMRAwardEmoji projectID(%d), err(%+v)", projectID, err)226 return227 }228 log.Info(">>>>>>>>> SyncMRAwardEmoji projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)229 totalPage = totalPage + page230 totalNum = totalNum + num231 }232 log.Info("===================== SyncMRAwardEmoji finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)233 return234}235// SyncMRDiscussion ...236func (s *Service) SyncMRDiscussion() (err error) {237 var (238 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs239 totalPage int240 totalNum int241 page int242 num int243 )244 log.Info("===================== SyncMRDiscussion start ========================")245 for _, projectID := range projectIDs {246 if page, num, err = s.SyncProjectDiscussion(context.TODO(), projectID); err != nil {247 log.Error("SyncMRDiscussion projectID(%d), err(%+v)", projectID, err)248 return249 }250 log.Info(">>>>>>>>> SyncMRDiscussion projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)251 totalPage = totalPage + page252 totalNum = totalNum + num253 }254 log.Info("===================== SyncMRDiscussion finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)255 return256}257// SyncRunners ...258func (s *Service) SyncRunners() (err error) {259 var (260 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs261 totalPage int262 totalNum int263 page int264 num int265 )266 log.Info("===================== SyncRunners start ========================")267 for _, projectID := range projectIDs {268 if page, num, err = s.SyncAllRunners(projectID); err != nil {269 log.Error("SyncRunners projectID(%d), err(%+v)", projectID, err)270 return271 }272 log.Info(">>>>>>>>> SyncRunners projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)273 totalPage = totalPage + page274 totalNum = totalNum + num275 }276 log.Info("===================== SyncRunners finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)277 return278}279// SyncIssues ...280func (s *Service) SyncIssues() (err error) {281 var (282 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs283 totalPage int284 totalNum int285 page int286 num int287 )288 log.Info("===================== SyncIssues start ========================")289 for _, projectID := range projectIDs {290 if page, num, err = s.SyncAllIssues(projectID); err != nil {291 log.Error("SyncIssues projectID(%d), err(%+v)", projectID, err)292 return293 }294 log.Info(">>>>>>>>> SyncIssues projectID(%d) complete Page(%d), Num(%d)", projectID, page, num)295 totalPage = totalPage + page296 totalNum = totalNum + num297 }298 log.Info("===================== SyncIssues finished totalPage(%d), totalNum(%d)========================", totalPage, totalNum)299 return300}301// SyncBranch ...302func (s *Service) SyncBranch() (err error) {303 var (304 projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs305 allPage int306 allNum int307 result *model.SyncResult308 )309 log.Info("===================== SyncBranch start ========================")310 for _, projectID := range projectIDs {311 if result, err = s.SyncProjectBranch(context.TODO(), projectID); err != nil {312 log.Error("SyncBranch projectID(%d), err(%+v)", projectID, err)313 go s.WechatFailData(model.DataTypeBranch, projectID, result, err)314 return315 }316 log.Info(">>>>>>>>> SyncBranch projectID(%d) complete Page(%d), Num(%d)", projectID, result.TotalPage, result.TotalNum)317 if result != nil && len(result.FailData) > 0 {318 go s.WechatFailData(model.DataTypeBranch, projectID, result, nil)319 }320 allPage = allPage + result.TotalPage321 allNum = allNum + result.TotalNum322 }323 log.Info("===================== SyncBranch finished totalPage(%d), totalNum(%d)========================", allPage, allNum)324 return325}326// AggregateBranch ...327func (s *Service) AggregateBranch() (err error) {328 var projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs329 log.Info("===================== SyncAggregateBranch start ========================")330 for _, projectID := range projectIDs {331 if err = s.AggregateProjectBranch(context.TODO(), projectID, _baseBranch); err != nil {332 log.Error("SyncAggregateBranch projectID(%d), err(%+v)", projectID, err)333 return334 }335 }336 log.Info("===================== SyncAggregateBranch finished ========================")337 return...

Full Screen

Full Screen

paginator.go

Source:paginator.go Github

copy

Full Screen

...26 err error27}28// Result defines a paginated result.29type Result struct {30 CurrentPage int `json:"currentPage"`31 MaxPage int `json:"maxPage"`32 RecordsPerPage int `json:"recordsPerPage"`33 TotalRecords int `json:"totalRecords"`34 Records interface{} `json:"records"`35}36// New create a new value of the Paginator type. It expects a gorm DB handle37// and pagination options.38// var v []SomeModel39// p := paginator.New(db, paginator.WithPage(2))40// res, err := p.Paginate(&v)41func New(db *gorm.DB, options ...Option) Paginator {42 p := &paginator{43 db: db,44 page: 1,45 limit: DefaultLimit,46 order: make([]string, 0),47 }48 for _, option := range options {49 option(p)50 }51 return p52}53// Paginate is a convenience wrapper for the paginator.54// var v []SomeModel55// res, err := paginator.Paginate(db, &v, paginator.WithPage(2))56func Paginate(db *gorm.DB, value interface{}, options ...Option) (*Result, error) {57 return New(db, options...).Paginate(value)58}59// Paginate implements the Paginator interface.60func (p *paginator) Paginate(value interface{}) (*Result, error) {61 db := p.prepareDB()62 c := make(chan countResult, 1)63 go countRecords(db, value, c)64 err := db.Limit(p.limit).Offset(p.offset()).Find(value).Error65 if err != nil {66 <-c67 return nil, err68 }69 return p.result(value, <-c)70}71// prepareDB prepares the statement by adding the order clauses.72func (p *paginator) prepareDB() *gorm.DB {73 db := p.db74 for _, o := range p.order {75 db = db.Order(o)76 }77 return db78}79// offset computes the offset used for the paginated query.80func (p *paginator) offset() int {81 return (p.page - 1) * p.limit82}83// countRecords counts the result rows for given query and returns the result84// in the provided channel.85func countRecords(db *gorm.DB, value interface{}, c chan<- countResult) {86 var result countResult87 result.err = db.Model(value).Count(&result.total).Error88 c <- result89}90// result creates a new Result out of the retrieved value and the count query91// result.92func (p *paginator) result(value interface{}, c countResult) (*Result, error) {93 if c.err != nil {94 return nil, c.err95 }96 maxPageF := float64(c.total) / float64(p.limit)97 maxPage := int(maxPageF)98 if float64(maxPage) < maxPageF {99 maxPage++100 } else if maxPage == 0 {101 maxPage = 1102 }103 return &Result{104 TotalRecords: c.total,105 Records: value,106 CurrentPage: p.page,107 RecordsPerPage: p.limit,108 MaxPage: maxPage,109 }, nil110}111// IsLastPage returns true if the current page of the result is the last page.112func (r *Result) IsLastPage() bool {113 return r.CurrentPage >= r.MaxPage114}115// IsFirstPage returns true if the current page of the result is the first page.116func (r *Result) IsFirstPage() bool {117 return r.CurrentPage <= 1118}...

Full Screen

Full Screen

Page

Using AI Code Generation

copy

Full Screen

1func main() {2 result := Result{Page: 1, Fruits: []string{"apple", "peach", "pear"}}3 t, _ := template.ParseFiles("tmpl.html")4 t.Execute(os.Stdout, result)5}6{{.Page}}7{{range .Fruits}}8 {{.}}9{{end}}10func main() {11 result := Result{Page: 1, Fruits: []string{"apple", "peach", "pear"}}12 t, _ := template.ParseFiles("tmpl.html")13 t.Execute(os.Stdout, result)14}15{{.Page}}16{{range .Fruits}}17 {{.}}18{{end}}19func main() {20 result := Result{Page: 1, Fruits: []string{"apple", "peach", "pear"}}21 t, _ := template.ParseFiles("tmpl.html")22 t.Execute(os.Stdout, result)23}24{{.Page}}25{{range .Fruits}}26 {{.}}27{{end}}28func main() {29 result := Result{Page: 1, Fruits: []string{"apple", "peach", "pear"}}30 t, _ := template.ParseFiles("tmpl.html")31 t.Execute(os.Stdout, result)32}33{{.Page}}34{{range .Fruits}}35 {{.}}36{{end}}37func main() {38 result := Result{Page: 1, Fruits: []string{"apple", "peach", "pear"}}39 t, _ := template.ParseFiles("tmpl.html")40 t.Execute(os.Stdout, result)41}42{{.Page}}43{{range .Fruits}}44 {{.}}45{{end}}

Full Screen

Full Screen

Page

Using AI Code Generation

copy

Full Screen

1import (2type Result struct {3}4func (r *Result) Page() (*goquery.Document, error) {5 response, err := http.Get(r.Url)6 if err != nil {7 }8 defer response.Body.Close()9 if response.StatusCode != 200 {10 return nil, fmt.Errorf("status code error: %d %s", response.StatusCode, response.Status)11 }12 return goquery.NewDocumentFromReader(response.Body)13}14func main() {15 result := &Result{16 }17 doc, err := result.Page()18 if err != nil {19 panic(err)20 }21 fmt.Println(doc.Find("title").Text())22}

Full Screen

Full Screen

Page

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 c := colly.NewCollector()4 c.OnHTML("div", func(e *colly.HTMLElement) {5 e.ForEach("a[href]", func(_ int, e *colly.HTMLElement) {6 link := e.Attr("href")7 fmt.Println("Link found:", link)8 e.Request.Visit(link)9 })10 })11}

Full Screen

Full Screen

Page

Using AI Code Generation

copy

Full Screen

1import (2func main() {3if err != nil {4fmt.Println(err)5}6var page string = htmlquery.InnerText(htmlquery.FindOne(result, "/html/body"))7fmt.Println(page)8}

Full Screen

Full Screen

Page

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 result := new(xlsx.Result)4 result.Page(1, 1)5 result.Page(2, 1)6 result.Page(3, 1)7 result.Page(4, 1)8 result.Page(5, 1)9 result.Page(6, 1)10 result.Page(7, 1)11 result.Page(8, 1)12 result.Page(9, 1)13 result.Page(10, 1)14 result.Page(11, 1)15 result.Page(12, 1)16 result.Page(13, 1)17 result.Page(14, 1)18 result.Page(15, 1)19 result.Page(16, 1)20 result.Page(17, 1)21 result.Page(18, 1)22 result.Page(19, 1)23 result.Page(20, 1)24 result.Page(21, 1)25 result.Page(22, 1)26 result.Page(23, 1)27 result.Page(24, 1)28 result.Page(25, 1)29 result.Page(26, 1)30 result.Page(27, 1)31 result.Page(28, 1)32 result.Page(29, 1)33 result.Page(30, 1)34 result.Page(31, 1)35 result.Page(32, 1)36 result.Page(33, 1)37 result.Page(34, 1)38 result.Page(35, 1)39 result.Page(36, 1)40 result.Page(37, 1)41 result.Page(38, 1)42 result.Page(39, 1)43 result.Page(40, 1)44 result.Page(41, 1)45 result.Page(42, 1)46 result.Page(43, 1)47 result.Page(44, 1)48 result.Page(45, 1)49 result.Page(46, 1)50 result.Page(47, 1)51 result.Page(48, 1)52 result.Page(49, 1)53 result.Page(50, 1)54 result.Page(51, 1)55 result.Page(52,

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful