Explore this module further in the Mover Registry: @sage/shared
Structs
Favorite
Data on relationship created between a User
and the content they want to favorite.
public struct Favorite has store
count: u64,
created_at: u64,
is_favorite: bool,
updated_at: u64
Favorites
All of the relationships that exist between User
s and the content they have favorited.
public struct Favorites has store
current_favorites: u64,
favorites: sui::table::Table<address, Favorite>
Constants
Error code when attempting to favorite content that has already been favorited.
const EAlreadyFavorited: u64 = 370;
Error code when attempting to unfavorite content that has not been favorited.
const EIsNotFavorite: u64 = 371;
Functions
add
Favorite the content.
public fun add(favorites: &mut Favorites, key: address, timestamp: u64): u64
public fun add(
favorites: &mut Favorites,
key: address,
timestamp: u64
): u64 {
let does_exist = favorites.favorites.contains(key);
let mut favorite_count = 1;
if (does_exist) {
let favorite = favorites.favorites.borrow_mut(key);
assert!(!favorite.is_favorite, EAlreadyFavorited);
favorite.count = favorite.count + 1;
favorite.is_favorite = true;
favorite.updated_at = timestamp;
favorite_count = favorite.count;
} else {
let favorite = Favorite {
count: favorite_count,
created_at: timestamp,
is_favorite: true,
updated_at: timestamp
};
favorites.favorites.add(key, favorite);
};
favorites.current_favorites = favorites.current_favorites + 1;
favorite_count
}
assert_has_not_favorited
Aborts with EAlreadyFavorited
if the content has already been favorited.
public fun assert_has_not_favorited(favorites: &Favorites, key: address)
public fun assert_has_not_favorited(
favorites: &Favorites,
key: address
) {
let is_favorite = is_favorite(
favorites,
key
);
assert!(!is_favorite, EAlreadyFavorited);
}
create
Creates a new instance of Favorites
.
public fun create(ctx: &mut TxContext): Favorites
public fun create(
ctx: &mut TxContext
): Favorites {
let favorites = Favorites {
current_favorites: 0,
favorites: table::new(ctx)
};
favorites
}
get_count
Gets the number of times the content has been favorited.
public fun get_count(favorites: &Favorites, key: address): u64
public fun get_count(
favorites: &Favorites,
key: address
): u64 {
favorites.favorites[key].count
}
get_created_at
Get timestamp of when the content was first favorited.
public fun get_created_at(favorites: &Favorites, key: address): u64
public fun get_created_at(
favorites: &Favorites,
key: address
): u64 {
favorites.favorites[key].created_at
}
get_length
Get the number of favorites on the content currently.
public fun get_length(favorites: &Favorites): u64
public fun get_length(
favorites: &Favorites
): u64 {
favorites.current_favorites
}
get_updated_at
Get timestamp of when the content was most recently favorited.
public fun get_updated_at(favorites: &Favorites, key: address): u64
public fun get_updated_at(
favorites: &Favorites,
key: address
): u64 {
favorites.favorites[key].updated_at
}
is_favorite
Returns true or false based on whether the content has currently been favorited.
public fun is_favorite(favorites: &Favorites, key: address): bool
public fun is_favorite(
favorites: &Favorites,
key: address
): bool {
favorites.favorites.contains(key) &&
favorites.favorites[key].is_favorite
}
remove
Unfavorite the content.
public fun remove(favorites: &mut Favorites, key: address, timestamp: u64): u64
public fun remove(
favorites: &mut Favorites,
key: address,
timestamp: u64
): u64 {
let favorite = favorites.favorites.borrow_mut(key);
assert!(favorite.is_favorite, EIsNotFavorite);
favorite.is_favorite = false;
favorite.updated_at = timestamp;
favorites.current_favorites = favorites.current_favorites - 1;
favorite.count
}