Major Job Listings refactor

This commit is contained in:
2024-06-16 14:04:12 -05:00
parent 9076765aae
commit 32e3cc574c
11 changed files with 2147 additions and 956 deletions
+99 -4
View File
@@ -65,8 +65,8 @@ class Business {
}
class JobListing {
String? id;
String? businessId;
int? id;
int? businessId;
String name;
String description;
JobType type;
@@ -188,6 +188,26 @@ void main() async {
},
);
});
app.get('/fbla-api/businessdata/businessnames', (Request request) async {
print('business names request received');
var postgresResult = (await postgres.query('''
SELECT json_agg(
json_build_object(
'id', id,
'name', name
)
) FROM public.businesses
'''))[0][0];
return Response.ok(
json.encode(postgresResult),
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'
},
);
});
app.get('/fbla-api/businessdata/business/<business>',
(Request request, String business) async {
print('idividual business data request received');
@@ -209,6 +229,7 @@ void main() async {
json_agg(
json_build_object(
'id', l.id,
'businessId', l."businessId",
'name', l.name,
'description', l.description,
'type', l.type,
@@ -231,11 +252,85 @@ void main() async {
},
);
});
app.get('/fbla-api/businessdata/businesses', (Request request) async {
print('list of business data request received');
if (request.url.queryParameters['businesses'] == null) {
return Response.badRequest(
body: 'query \'businesses\' required',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'
},
);
}
var filters = request.url.queryParameters['businesses']!.split(',');
var result = (await postgres.query('''
SELECT
json_build_object(
'id', b.id,
'name', b.name,
'description', b.description,
'website', b.website,
'contactName', b."contactName",
'contactEmail', b."contactEmail",
'contactPhone', b."contactPhone",
'notes', b.notes,
'locationName', b."locationName",
'locationAddress', b."locationAddress",
'listings',
json_agg(
json_build_object(
'id', l.id,
'businessId', l."businessId",
'name', l.name,
'description', l.description,
'type', l.type,
'wage', l.wage,
'link', l.link
)
)
)
FROM businesses b
LEFT JOIN listings l ON b.id = l."businessId"
WHERE b.id IN ${'$filters'.replaceAll('[', '(').replaceAll(']', ')')}
GROUP BY b.id;
'''));
var output = result.map((element) => element[0]).toList();
return Response.ok(
json.encode(output),
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'
},
);
});
app.get('/fbla-api/businessdata', (Request request) async {
print('business data request received');
final output = await fetchBusinessData();
final result = await postgres.query('''
SELECT json_agg(
json_build_object(
'id', id,
'name', name,
'description', description,
'type', type,
'website', website,
'contactName', "contactName",
'contactEmail', "contactEmail",
'contactPhone', "contactPhone",
'notes', notes,
'locationName', "locationName",
'locationAddress', "locationAddress"
)
) FROM businesses
''');
var encoded = json.encode(result[0][0]);
return Response.ok(
output.toString(),
encoded,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/plain'