Skip to content

Commit

Permalink
Incorporate review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Jul 27, 2023
1 parent 025a652 commit 8711761
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion rest-social-media/ballerina/response_error_interceptor.bal
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ballerina/constraint;
service class ResponseErrorInterceptor {
*http:ResponseErrorInterceptor;

remote function interceptResponseError(http:RequestContext ctx, error err)
remote function interceptResponseError(error err)
returns SocialMediaBadReqeust|SocialMediaServerError {
ErrorDetails errorDetails = buildErrorPayload(err.message(), "");

Expand Down
16 changes: 9 additions & 7 deletions rest-social-media/ballerina/social_media_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ type DataBaseConfig record {|
|};
configurable DataBaseConfig databaseConfig = ?;

listener http:Listener socialMediaListener = new (9090,
interceptors = [new ResponseErrorInterceptor()]
);
listener http:Listener socialMediaListener = new (9090);

service SocialMedia /social\-media on socialMediaListener {

Expand All @@ -51,6 +49,11 @@ service SocialMedia /social\-media on socialMediaListener {
);
log:printInfo("Social media service started");
}

// Service-level error interceptors can handle errors occurred during the service execution.
public function createInterceptors() returns ResponseErrorInterceptor {
return new ResponseErrorInterceptor();
}

# Get all the users
#
Expand All @@ -73,16 +76,15 @@ service SocialMedia /social\-media on socialMediaListener {
body: errorDetails
};
return userNotFound;
} else {
return result;
}
return result;
}

# Create a new user
#
# + newUser - The user details of the new user
# + return - The created message or error message
resource function post users(@http:Payload NewUser newUser) returns http:Created|error {
resource function post users(NewUser newUser) returns http:Created|error {
_ = check self.socialMediaDb->execute(`
INSERT INTO social_media_database.user(birth_date, name)
VALUES (${newUser.birthDate}, ${newUser.name});`);
Expand Down Expand Up @@ -123,7 +125,7 @@ service SocialMedia /social\-media on socialMediaListener {
#
# + id - The user ID for which the post is created
# + return - The created message or error message
resource function post users/[int id]/posts(@http:Payload NewPost newPost) returns http:Created|UserNotFound|PostForbidden|error {
resource function post users/[int id]/posts(NewPost newPost) returns http:Created|UserNotFound|PostForbidden|error {
User|error result = self.socialMediaDb->queryRow(`SELECT * FROM social_media_database.user WHERE id = ${id}`);
if result is sql:NoRowsError {
ErrorDetails errorDetails = buildErrorPayload(string `id: ${id}`, string `users/${id}/posts`);
Expand Down

0 comments on commit 8711761

Please sign in to comment.