-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsocial_media_service.bal
122 lines (107 loc) · 3.87 KB
/
social_media_service.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import ballerinax/nats;
import ballerina/regex;
import ballerina/http;
import ballerina/sql;
import ballerinax/mysql.driver as _;
import ballerinax/mysql;
import ballerina/log;
import ballerinax/jaeger as _;
import ballerina/time;
configurable boolean moderate = ?;
configurable boolean enableSmsNotification = ?;
type DataBaseConfig record {|
string host;
int port;
string user;
string password;
string database;
|};
configurable DataBaseConfig databaseConfig = ?;
final mysql:Client socialMediaDb = check initDbClient();
function initDbClient() returns mysql:Client|error => new (...databaseConfig);
final http:Client sentimentEndpoint = check new ("localhost:9099",
retryConfig = {
interval: 3
},
secureSocket = {
cert: "./resources/public.crt"
}
);
listener http:Listener socialMediaListener = new (9090,
interceptors = [new ResponseErrorInterceptor()]
);
service SocialMedia /social\-media on socialMediaListener {
public function init() returns error? {
log:printInfo("Social media service started");
}
# Get all the users
#
# + return - The list of users or error message
resource function get users() returns User[]|error {
stream<User, sql:Error?> userStream = socialMediaDb->query(`SELECT * FROM users`);
return from User user in userStream
select user;
}
# Get a specific user
#
# + id - The user ID of the user to be retrived
# + return - A specific user or error message
resource function get users/[int id]() returns User|UserNotFound|error {
User|error result = socialMediaDb->queryRow(`SELECT * FROM users WHERE ID = ${id}`);
if result is sql:NoRowsError {
ErrorDetails errorDetails = buildErrorPayload(string `id: ${id}`, string `users/${id}/posts`);
UserNotFound userNotFound = {
body: errorDetails
};
return userNotFound;
} else {
return result;
}
}
# Create a post for a given user
#
# + 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 {
User|error user = socialMediaDb->queryRow(`SELECT * FROM users WHERE id = ${id}`);
if user is sql:NoRowsError {
ErrorDetails errorDetails = buildErrorPayload(string `id: ${id}`, string `users/${id}/posts`);
UserNotFound userNotFound = {
body: errorDetails
};
return userNotFound;
}
if user is error {
return user;
}
Sentiment sentiment = check sentimentEndpoint->/text\-processing/api/sentiment.post(
{text: newPost.description}
);
if sentiment.label == "neg" {
ErrorDetails errorDetails = buildErrorPayload(string `id: ${id}`, string `users/${id}/posts`);
PostForbidden postForbidden = {
body: errorDetails
};
return postForbidden;
}
_ = check socialMediaDb->execute(`
INSERT INTO posts(description, category, created_date, tags, user_id)
VALUES (${newPost.description}, ${newPost.category}, CURDATE(), ${newPost.tags}, ${id});`);
return http:CREATED;
}
}
function buildErrorPayload(string msg, string path) returns ErrorDetails => {
message: msg,
timeStamp: time:utcNow(),
details: string `uri=${path}`
};
function mapPostToPostWithMeta(Post[] post) returns PostWithMeta[] => from var postItem in post
select {
id: postItem.id,
description: postItem.description,
meta: {
tags: regex:split(postItem.tags, ","),
category: postItem.category,
created_date: postItem.created_date
}
};