// book_model.
dart
import 'package:supabase_flutter/supabase_flutter.dart';
class Book {
final String id;
final String title;
final String author;
final DateTime publishedDate;
bool isAvailable;
Book({
required [Link],
required [Link],
required [Link],
required [Link],
required [Link],
});
factory [Link](Map<String, dynamic> json) {
return Book(
id: json['id'],
title: json['title'],
author: json['author'],
publishedDate: [Link](json['published_date']),
isAvailable: json['is_available'],
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'title': title,
'author': author,
'published_date': publishedDate.toIso8601String(),
'is_available': isAvailable,
};
}
}
// book_service.dart
class BookService {
final SupabaseClient supabase;
BookService([Link]);
// Menambahkan buku baru
Future<Book> addBook(Book book) async {
final response = await supabase
.from('books')
.insert([Link]())
.select()
.single();
return [Link](response);
}
// Mengambil semua buku
Future<List<Book>> getAllBooks() async {
final response = await supabase
.from('books')
.select()
.order('title');
return [Link]((json) => [Link](json)).toList();
}
// Memperbarui status ketersediaan buku
Future<void> updateBookAvailability(String id, bool isAvailable) async {
await supabase
.from('books')
.update({'is_available': isAvailable})
.eq('id', id);
}
// Menghapus buku
Future<void> deleteBook(String id) async {
await supabase
.from('books')
.delete()
.eq('id', id);
}
// Subscribe ke perubahan real-time
Stream<List<Book>> getBooksStream() {
return supabase
.from('books')
.stream(primaryKey: ['id'])
.map((list) => [Link]((json) => [Link](json)).toList());
}
}
// books_screen.dart
class BooksScreen extends StatefulWidget {
@override
_BooksScreenState createState() => _BooksScreenState();
}
class _BooksScreenState extends State<BooksScreen> {
final BookService _bookService = BookService([Link]);
late Stream<List<Book>> _booksStream;
@override
void initState() {
[Link]();
_booksStream = _bookService.getBooksStream();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Library Management'),
),
body: StreamBuilder<List<Book>>(
stream: _booksStream,
builder: (context, snapshot) {
if ([Link]) {
return Center(child: Text('Error: ${[Link]}'));
}
if (![Link]) {
return Center(child: CircularProgressIndicator());
}
final books = [Link]!;
return [Link](
itemCount: [Link],
itemBuilder: (context, index) {
final book = books[index];
return ListTile(
title: Text([Link]),
subtitle: Text([Link]),
trailing: Switch(
value: [Link],
onChanged: (bool value) {
_bookService.updateBookAvailability([Link], value);
},
),
onLongPress: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Delete Book'),
content: Text('Are you sure you want to delete this book?'),
actions: [
TextButton(
onPressed: () => [Link](context),
child: Text('Cancel'),
),
TextButton(
onPressed: () {
_bookService.deleteBook([Link]);
[Link](context);
},
child: Text('Delete'),
),
],
),
);
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Implement add book dialog/screen
},
child: Icon([Link]),
),
);
}
}