-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.v
48 lines (40 loc) · 1.3 KB
/
services.v
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
module services
import structs as s
import requests as r
import repository as repo
import httperrors as he
import time
import log
__global (db = repo.setup())
pub fn create_link(req_body r.CreateLinkRequest) s.ResponseObject<s.Response> {
log.info('[SERVICE] - create_link function')
if !db.save(req_body.link_name, req_body.link_url) {
log.error('lost connection with database')
return he.create_response_error('unable to reach database', 500)
}
created_link := s.Response(s.CreatedLink{
link_name: req_body.link_name
creation_date: time.now().hhmmss()
})
return s.ResponseObject<s.Response>{
code: 201
body: created_link
format: s.FileFormats.json
}
}
pub fn get_link_url(link_name string) s.ResponseObject<s.Response> {
log.info('[SERVICE] - get_link_url function')
result := db.get(link_name)
if !result.success {
log.warn('${link_name} not found')
return he.create_response_error('url not found', 404)
}
page_link := result.data
script := '<script>window.location.href = "${page_link}"</script>'
response := s.Response(script)
return s.ResponseObject<s.Response>{
code: 200
body: response
format: s.FileFormats.html
}
}