(req ProofQueryRequest)
| 77 | } |
| 78 | |
| 79 | func performProofQuery(req ProofQueryRequest) ([]ProofQueryResponseSingle, ProofQueryPaginationResponse) { |
| 80 | pagination := ProofQueryPaginationResponse{ |
| 81 | Total: 0, |
| 82 | Per: PER_PAGE, |
| 83 | Current: req.Page, |
| 84 | Next: 0, |
| 85 | } |
| 86 | if pagination.Current <= 0 { // `page` param not provided. Set it to 1. |
| 87 | pagination.Current = 1 |
| 88 | } |
| 89 | offsetCount := pagination.Per * (pagination.Current - 1) |
| 90 | |
| 91 | result := make([]ProofQueryResponseSingle, 0, 0) |
| 92 | proofs := make([]model.Proof, 0, 0) |
| 93 | tx := model.ReadOnlyDB.Model(&model.Proof{}) |
| 94 | |
| 95 | // support selected fields only. |
| 96 | orderBy := "id" |
| 97 | order := "desc" |
| 98 | switch strings.ToLower(req.SortBy) { |
| 99 | case "id", "last_arweave_id", "created_at", "last_checked_at", "proof_chain_id", "platform", "identity", "alt_id": |
| 100 | orderBy = strings.ToLower(req.SortBy) |
| 101 | } |
| 102 | switch strings.ToLower(req.Order) { |
| 103 | case "asc", "desc": |
| 104 | order = strings.ToLower(req.Order) |
| 105 | } |
| 106 | |
| 107 | if strings.ToLower(req.SortBy) == "activated_at" { |
| 108 | orderBy = "created_at" |
| 109 | tx = model.ReadOnlyDB.Table("proof"). |
| 110 | Select("proof.*, proof_chains.*"). |
| 111 | Joins("INNER JOIN proof_chains ON proof.persona = proof_chains.persona"). |
| 112 | Order("proof_chains.created_at " + order). |
| 113 | Model(&model.Proof{}) |
| 114 | } else { |
| 115 | tx = tx.Order(orderBy + " " + order) |
| 116 | } |
| 117 | |
| 118 | switch req.Platform { |
| 119 | case string(types.Platforms.NextID): |
| 120 | { |
| 121 | tx = tx.Where("proof.persona IN ?", req.Identity).Offset(offsetCount).Limit(pagination.Per).Find(&proofs) |
| 122 | pagination.Total = tx.RowsAffected |
| 123 | } |
| 124 | case "": |
| 125 | { // All platform |
| 126 | if req.ExactMatch { |
| 127 | tx = tx.Where("proof.identity = ? OR proof.alt_id = ?", strings.ToLower(req.Identity[0]), strings.ToLower(req.Identity[0])) |
| 128 | } else { |
| 129 | tx = tx.Where("proof.identity LIKE ? OR proof.alt_id LIKE ?", "%"+strings.ToLower(req.Identity[0])+"%", "%"+strings.ToLower(req.Identity[0])+"%") |
| 130 | } |
| 131 | |
| 132 | for i, id := range req.Identity { |
| 133 | if i == 0 { |
| 134 | continue |
| 135 | } |
| 136 | if req.ExactMatch { |
no test coverage detected