Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
omarbabaiev committed Jul 25, 2024
1 parent ca898ed commit b1e6148
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions bin/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,25 @@ Future<void> loadData() async {

Response _getAllCategories(Request req) {
try {
// Kategorilerin listesini al
var categories = data["categories"] as List;

// Kategori id ve isimlerini çıkart
var categoryDetails = categories.map((category) {
return {
"id": category["id"],
"name": category["name"]
};
}).toList();

// JSON formatında döndür
return Response.ok(
jsonEncode({"categories": categoryDetails}),
headers: {'Content-Type': 'application/json'}
);
} catch (e) {
// Hata durumunda yanıt döndür
return Response.internalServerError(
body: 'Failed to get categories: $e'
);
}
}

Response _getQuoteById(Request req, String quoteId) {
try {
var categories = data["categories"] as List?;
Expand All @@ -52,14 +48,12 @@ Response _getQuoteById(Request req, String quoteId) {
var quotes = subcategory["quotes"] as List?;
if (quotes == null) continue;

// Alıntıyı id'ye göre bul
var quote = quotes.firstWhere(
(q) => q["id"].toString() == quoteId,
orElse: () => {"id": "", "text": "", "author": ""} // Boş bir Map döndür
orElse: () => {"id": "", "text": "", "author": ""}
);

if (quote["id"] != "") { // Eğer boş değilse
// Alıntıyı, id'sini ve yazarını döndür
if (quote["id"] != "") {
return Response.ok(
jsonEncode({
"quote": {
Expand All @@ -81,6 +75,7 @@ Response _getQuoteById(Request req, String quoteId) {
);
}
}

Response _getAuthors(Request req) {
try {
var categories = data["categories"];
Expand Down Expand Up @@ -112,6 +107,7 @@ Response _getAuthors(Request req) {
return Response.internalServerError(body: 'Failed to get authors: $e');
}
}

Response _getQuotesByAuthor(Request req, String authorName) {
try {
var categories = data["categories"] as List?;
Expand Down Expand Up @@ -146,6 +142,7 @@ Response _getQuotesByAuthor(Request req, String authorName) {
return Response.internalServerError(body: 'Failed to get quotes by author: $e');
}
}

Response _getSubcategoriesByCategory(Request req, String categoryName) {
try {
var categories = data["categories"] as List;
Expand Down Expand Up @@ -178,17 +175,21 @@ Response _getSubcategoriesByCategory(Request req, String categoryName) {
}
}

Response _home(Request req) {
return Response.ok('Welcome to the Quotes API! Use the available endpoints to get quotes, authors, and more.',
headers: {'Content-Type': 'text/plain'});
}

void main() async {
await loadData();
final router = Router()
..get('/', _home)
..get('/categories', _getAllCategories)
..get('/quotes/<quoteId>', _getQuoteById)
..get('/quotes/author/<authorName>', _getQuotesByAuthor)
..get('/authors', _getAuthors)
..get('/subcategories/category/<categoryName>', _getSubcategoriesByCategory);



final handler = const Pipeline()
.addMiddleware(logRequests())
.addHandler(router);
Expand Down

0 comments on commit b1e6148

Please sign in to comment.