-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartistFunctions.cpp
More file actions
149 lines (123 loc) · 4.24 KB
/
artistFunctions.cpp
File metadata and controls
149 lines (123 loc) · 4.24 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <crow.h>
#include <map>
#include <string>
#include "Artist.h"
using namespace std;
using namespace crow;
extern map<string, Artist> artistMap;
response createArtist(request req)
{
string apiKeyHeader = "Authorization";
string expectedApiKey = "Key";
// Validate the api key in the request header.
if (!req.headers.count(apiKeyHeader) || req.headers.find(apiKeyHeader)->second != expectedApiKey)
{
return response(403, "Forbidden");
}
// Load the request body string into a JSON read value.
json::rvalue readValueJson = json::load(req.body);
// If there was a problem converting the body text to JSON return an error.
if (!readValueJson)
return response(400, "Bad Request");
// Create a new artist.
Artist artist{readValueJson["name"].s(), readValueJson["type"].s(), readValueJson["cost"].d()};
// Add the new Artist to the map.
artistMap[artist.getName()] = artist;
// Return the created artist as a JSON string.
return response(201, artist.convertToJson().dump());
}
response readArtist(string name)
{
try
{
// Get the artist from the artist map.
Artist artist = artistMap.at(name);
// Return the artist as a JSON string.
return response(artist.convertToJson().dump());
}
catch (out_of_range& exception)
{
// If the artist was not found in the map return a 404 not found error.
return response(404, "Artist Not Found");
}
}
response readAllArtists(request req)
{
// Create a new JSON write value use to write to the file.
json::wvalue jsonWriteValue;
// For each artist in the map, convert the artist to JSON and add to the write value.
int index = 0;
for (pair<string, Artist> keyValuePair : artistMap)
{
// first: gives you access to the first item in the pair.
// second: gives you access to the second item in the pair.
jsonWriteValue[index] =keyValuePair.second.convertToJson();
index++;
}
return response(jsonWriteValue.dump());
}
void updateArtist(request req, response& res, string name)
{
string apiKeyHeader = "Authorization";
string expectedApiKey = "Key";
// Validate the api key in the request header.
if (!req.headers.count(apiKeyHeader) || req.headers.find(apiKeyHeader)->second != expectedApiKey)
{
res.code = 403; // Unauthorized
res.end("Forbidden");
return;
}
try
{
// Get the artist from the artist map.
Artist artist = artistMap.at(name);
// Convert the request body to JSON.
json::rvalue readValueJson = json::load(req.body);
// If there was a problem converting the body text to JSON return an error.
if (!readValueJson)
{
res.code = 400;
res.end("Invalid JSON");
return;
}
// Update the artist.
artist.setDescription(readValueJson["type"].s());
artist.setCost(readValueJson["cost"].d());
artistMap[name] = artist;
// Return the updated artist as a JSON string.
res.code = 200;
res.set_header("Content-Type", "application/json");
res.write(artist.convertToJson().dump());
res.end();
}
catch (out_of_range& exception)
{
// If the artist was not found in the map return a 404 not found error.
res.code = 404;
res.end("Artist Not Found");
}
}
response deleteArtist(request req, string name)
{
string apiKeyHeader = "Authorization";
string expectedApiKey = "Key";
// Validate the api key in the request header.
if (!req.headers.count(apiKeyHeader) || req.headers.find(apiKeyHeader)->second != expectedApiKey)
{
return response(403, "Forbidden");
}
try
{
// Get the artist from the artist map.
Artist artist = artistMap.at(name);
// Remove the artist from the artist map.
artistMap.erase(name);
// Return a successful code 204 which means success but no content to return.
return response(204);
}
catch (out_of_range& exception)
{
// If the artist was not found in the map return a 404 not found error.
return response(404, "Artist not found");
}
}