github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/models/list.go (about)

     1  package models
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  
     7  	s "github.com/DapperCollectives/CAST/backend/main/shared"
     8  	"github.com/georgysavva/scany/pgxscan"
     9  )
    10  
    11  type List struct {
    12  	ID           int        `json:"id"`
    13  	Community_id int        `json:"communityId"`
    14  	Addresses    []string   `json:"addresses,omitempty" validate:"required"`
    15  	List_type    *string    `json:"listType,omitempty"`
    16  	Cid          *string    `json:"cid,omitempty"`
    17  	Created_at   *time.Time `json:"createdAt,omitempty"`
    18  }
    19  
    20  type ListPayload struct {
    21  	List
    22  	s.TimestampSignaturePayload
    23  }
    24  
    25  type ListUpdatePayload struct {
    26  	ID        int      `json:"id"`
    27  	Addresses []string `json:"addresses,omitempty" validate:"required"`
    28  	s.TimestampSignaturePayload
    29  }
    30  
    31  func GetListsForCommunity(db *s.Database, communityId int) ([]List, error) {
    32  	lists := []List{}
    33  	err := pgxscan.Select(db.Context, db.Conn, &lists,
    34  		`SELECT * FROM lists WHERE community_id = $1`,
    35  		communityId)
    36  
    37  	return lists, err
    38  }
    39  
    40  func GetListForCommunityByType(db *s.Database, communityId int, listType string) (List, error) {
    41  	var list = List{}
    42  	err := pgxscan.Get(db.Context, db.Conn, &list,
    43  		`SELECT * FROM lists WHERE community_id = $1 AND list_type = $2`,
    44  		communityId, listType)
    45  	return list, err
    46  }
    47  
    48  func (l *List) GetListById(db *s.Database) error {
    49  	return pgxscan.Get(db.Context, db.Conn, l, `
    50  		SELECT * FROM lists WHERE id = $1
    51  	`, l.ID)
    52  }
    53  
    54  func (l *List) CreateList(db *s.Database) error {
    55  	err := db.Conn.QueryRow(db.Context,
    56  		`
    57  		INSERT INTO lists(community_id, addresses, list_type, cid)
    58  		VALUES($1, $2, $3, $4)
    59  		RETURNING id, created_at
    60  	`, l.Community_id, l.Addresses, l.List_type, l.Cid).Scan(&l.ID, &l.Created_at)
    61  
    62  	return err // will be nil unless something went wrong
    63  }
    64  
    65  func (l *List) UpdateList(db *s.Database) error {
    66  	_, err := db.Conn.Exec(db.Context,
    67  		`
    68  		UPDATE lists
    69  		SET addresses = $1
    70  		WHERE id = $2
    71  	`, l.Addresses, l.ID)
    72  
    73  	return err // will be nil unless something went wrong
    74  }
    75  
    76  func (l *List) AddAddresses(addresses []string) {
    77  	// Put addresses into map to speed up lookup time
    78  	addrMap := map[string]bool{}
    79  	for _, addr := range l.Addresses {
    80  		addrMap[addr] = true
    81  	}
    82  	// only add addresses that havent been added
    83  	for _, addr := range addresses {
    84  		if !addrMap[addr] {
    85  			l.Addresses = append(l.Addresses, addr)
    86  		}
    87  	}
    88  }
    89  
    90  func (l *List) RemoveAddresses(toRemove []string) {
    91  	// remove specified addresses
    92  	for _, addr := range toRemove {
    93  		// Get the index of element to remove
    94  		i := sort.StringSlice(l.Addresses).Search(addr)
    95  		if i >= 0 {
    96  			// Remove the element at index i from a.
    97  			copy(l.Addresses[i:], l.Addresses[i+1:])       // Shift a[i+1:] left one index.
    98  			l.Addresses[len(l.Addresses)-1] = ""           // Erase last element (write zero value).
    99  			l.Addresses = l.Addresses[:len(l.Addresses)-1] // Truncate slice.
   100  		}
   101  	}
   102  }