@@ -15,6 +15,10 @@ abstract class IAuthorRepo {
1515 Future <Author ?> findAuthorByName (String name);
1616 Future <List <Author >> showAuthorPaging ({int offset = 0 , int limit = 8 });
1717 Future <int ?> createAuthor (Author author, String avatarPath);
18+ Future <Author ?> updateAuthor (
19+ int authorId,
20+ Map <String , dynamic > updateFields,
21+ );
1822}
1923
2024class AuthorRepository implements IAuthorRepo {
@@ -37,7 +41,9 @@ class AuthorRepository implements IAuthorRepo {
3741
3842 if (authorResult.isEmpty || authorResult.first.isEmpty) {
3943 throw const CustomHttpException (
40- ErrorMessage .AUTHOR_NOT_FOUND , HttpStatus .notFound,);
44+ ErrorMessage .AUTHOR_NOT_FOUND ,
45+ HttpStatus .notFound,
46+ );
4147 }
4248
4349 final authorRow = authorResult.first;
@@ -212,16 +218,19 @@ SELECT al.id,
212218 @override
213219 Future <List <Author >> showAuthorPaging ({int offset = 0 , int limit = 8 }) async {
214220 try {
215- final authorResult = await _db.executor.execute (Sql .named ('''
221+ final authorResult = await _db.executor.execute (
222+ Sql .named ('''
216223SELECT id, name,avatarUrl
217224FROM author
218225ORDER BY RANDOM()
219226LIMIT @limit
220227OFFSET @offset
221- ''' ), parameters: {
222- 'limit' : limit,
223- 'offset' : offset,
224- },);
228+ ''' ),
229+ parameters: {
230+ 'limit' : limit,
231+ 'offset' : offset,
232+ },
233+ );
225234
226235 if (authorResult.isEmpty) {
227236 return [];
@@ -288,6 +297,67 @@ OFFSET @offset
288297 }
289298 }
290299
300+ @override
301+ Future <Author ?> updateAuthor (
302+ int authorId,
303+ Map <String , dynamic > updateFields,
304+ ) async {
305+ try {
306+ final setClauseParts = < String > [];
307+ final parameters = < String , dynamic > {
308+ 'id' : authorId,
309+ 'updatedAt' : DateTime .now (),
310+ };
311+
312+ if (updateFields.containsKey ('name' )) {
313+ setClauseParts.add ('name = @name' );
314+ parameters['name' ] = updateFields['name' ];
315+ }
316+ if (updateFields.containsKey ('description' )) {
317+ setClauseParts.add ('description = @description' );
318+ parameters['description' ] = updateFields['description' ];
319+ }
320+ setClauseParts.add ('updatedAt = @updatedAt' );
321+ final setClause = setClauseParts.join (', ' );
322+ final query = '''
323+ UPDATE author
324+ SET $setClause
325+ WHERE id = @id
326+ RETURNING id,name,description,avatarUrl,createdAt,updatedAt,followingCount
327+ ''' ;
328+
329+ final result = await _db.executor.execute (
330+ Sql .named (query),
331+ parameters: parameters,
332+ );
333+ if (result.isEmpty || result.first.isEmpty) {
334+ throw const CustomHttpException (
335+ ErrorMessageSQL .SQL_QUERY_ERROR ,
336+ HttpStatus .internalServerError,
337+ );
338+ }
339+
340+ final row = result.first;
341+ return Author (
342+ id: row[0 ]! as int ,
343+ name: row[1 ]! as String ,
344+ description: row[2 ]! as String ,
345+ avatarUrl: row[3 ] as String ? ,
346+ followingCount: row[4 ]! as int ,
347+ createdAt: _parseDate (row[5 ]),
348+ updatedAt: _parseDate (row[6 ]),
349+ );
350+ } catch (e) {
351+ if (e is CustomHttpException ) {
352+ rethrow ;
353+ }
354+ throw const CustomHttpException (
355+ ErrorMessageSQL .SQL_QUERY_ERROR ,
356+ HttpStatus .internalServerError,
357+ );
358+ }
359+ }
360+
291361 DateTime ? _parseDate (dynamic date) {
292362 if (date == null ) {
293363 return null ;
0 commit comments