addd urlhandler for static content, more reusing of code.
This commit is contained in:
@@ -42,9 +42,13 @@ static GWEN_MSG *_handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
|
||||
static void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf);
|
||||
|
||||
static void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *pageBuf);
|
||||
static void _handleGetEdit(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id, GWEN_BUFFER *pageBuf);
|
||||
static GWEN_MSG *_handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
|
||||
static GWEN_MSG *_handlePostEdit(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id);
|
||||
static int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf);
|
||||
static int _writeEditPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf);
|
||||
static GWEN_MSG *_addRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq);
|
||||
static GWEN_MSG *_editRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id);
|
||||
|
||||
|
||||
|
||||
@@ -125,6 +129,8 @@ GWEN_MSG *_handleGet(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
_handleGetList(uh, rq, pageBuf);
|
||||
else if (strcasecmp(s, "add")==0)
|
||||
_handleGetAdd(uh, rq, pageBuf);
|
||||
else if (strcasecmp(s, "edit")==0)
|
||||
_handleGetEdit(uh, rq, GWEN_StringList_StringAsIntAt(sl, 2, 0), pageBuf);
|
||||
else {
|
||||
DBG_ERROR(NULL, "Invalid url (2nd member is [%s])", s);
|
||||
GWEN_Buffer_free(pageBuf);
|
||||
@@ -170,6 +176,8 @@ GWEN_MSG *_handlePost(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
if (s && *s) {
|
||||
if (strcasecmp(s, "add")==0)
|
||||
return _handlePostAdd(uh, rq);
|
||||
else if (strcasecmp(s, "edit")==0)
|
||||
return _handlePostEdit(uh, rq, GWEN_StringList_StringAsIntAt(sl, 2, 0));
|
||||
else {
|
||||
DBG_ERROR(NULL, "Invalid url (2nd member is [%s])", s);
|
||||
return AQH_HttpService_CreateResponseMsg(AQH_HttpUrlHandler_GetHttpService(uh), 404, "Not found", protocol, NULL);
|
||||
@@ -207,7 +215,7 @@ void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *
|
||||
"<h2>%s</h2>"
|
||||
"<table class=\"dataTable\">"
|
||||
"<thead>"
|
||||
" <tr><th>Id</th><th>%s</th><th>%s</th></tr>"
|
||||
" <tr><th>Id</th><th>%s</th><th>%s</th><th></th></tr>"
|
||||
"</thead>",
|
||||
I18N("Rooms"),
|
||||
I18N("Name"),
|
||||
@@ -226,10 +234,15 @@ void _handleGetList(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *
|
||||
id=(long unsigned int) AQH_Room_GetId(r);
|
||||
name=AQH_Room_GetName(r);
|
||||
descr=AQH_Room_GetDescription(r);
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<tr><td>%lu</td><td>%s</td><td>%s</td></tr>", id, name?name:"", descr?descr:"");
|
||||
|
||||
r=AQH_Room_List_Next(r);
|
||||
}
|
||||
GWEN_Buffer_AppendArgs(pageBuf,
|
||||
"<tr><td>%lu</td><td>%s</td><td>%s</td>"
|
||||
"<td><a href=\"/rooms/edit/%lu\">"
|
||||
"<IMG src=\"/pics/edit.png\" width=32 height=32 align=left border=0></a></td>"
|
||||
"</tr>",
|
||||
id, name?name:"", descr?descr:"", id);
|
||||
|
||||
r=AQH_Room_List_Next(r);
|
||||
}
|
||||
}
|
||||
GWEN_Buffer_AppendString(pageBuf, "</tbody>");
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "</table><a href=\"/rooms/add\">%s</a><br>", I18N("Add Room"));
|
||||
@@ -256,7 +269,61 @@ void _handleGetAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_BUFFER *p
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "No permissions to add a room.");
|
||||
GWEN_Buffer_AppendString(pageBuf, "<p>No permissions to add a room.</p>");
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"red\">%s</font></p>", I18N("No permissions to add a room."));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void _handleGetEdit(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id, GWEN_BUFFER *pageBuf)
|
||||
{
|
||||
uint32_t perms;
|
||||
|
||||
DBG_ERROR(NULL, "EDIT");
|
||||
perms=AQH_HttpRequest_GetModulePerms(rq);
|
||||
if (perms & AQHOME_HTTP_PERMS_EDIT_ROOM) {
|
||||
AQH_SERVICE *sv;
|
||||
AQH_STORAGE *sto;
|
||||
|
||||
sv=AQH_HttpUrlHandler_GetHttpService(uh);
|
||||
sto=AqHomeHttpService_GetStorage(sv);
|
||||
if (sto) {
|
||||
if (id>0) {
|
||||
const AQH_ROOM *r;
|
||||
|
||||
r=AQH_Storage_GetRoomById(sto, id);
|
||||
if (r) {
|
||||
GWEN_DB_NODE *db;
|
||||
int rv;
|
||||
|
||||
db=GWEN_DB_Group_new("room");
|
||||
rv=AQH_Room_toDb(r, db);
|
||||
if (rv>=0)
|
||||
_writeEditPage(uh, rq, NULL, pageBuf);
|
||||
else {
|
||||
DBG_ERROR(NULL, "Error writing room %d to db", id);
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"red\">%s</font></p>", I18N("Internal error."));
|
||||
}
|
||||
GWEN_DB_Group_free(db);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Room %d not found", id);
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"red\">%s</font></p>", I18N("Room not found."));
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "Missing room id");
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"red\">%s</font></p>", I18N("Missing or invalid room id."));
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "No storage");
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p><font color=\"red\">%s</font></p>", I18N("Internal error."));
|
||||
}
|
||||
}
|
||||
else {
|
||||
DBG_INFO(NULL, "No permissions to edit a room.");
|
||||
GWEN_Buffer_AppendArgs(pageBuf, "<p>%s</p>", I18N("No permissions to edit a room."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,6 +364,44 @@ int _writeAddPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *d
|
||||
|
||||
|
||||
|
||||
int _writeEditPage(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, GWEN_DB_NODE *dbValues, GWEN_BUFFER *pageBuf)
|
||||
{
|
||||
unsigned long int id;
|
||||
const char *name=NULL;
|
||||
const char *descr=NULL;
|
||||
|
||||
if (dbValues) {
|
||||
id=GWEN_DB_GetIntValue(dbValues, "id", 0, 0);
|
||||
name=GWEN_DB_GetCharValue(dbValues, "name", 0, NULL);
|
||||
descr=GWEN_DB_GetCharValue(dbValues, "description", 0, NULL);
|
||||
}
|
||||
GWEN_Buffer_AppendArgs(pageBuf,
|
||||
"<h2>%s</h2><br>\n"
|
||||
"<form action=\"/rooms/edit/%lu\" method=\"post\" enctype=\"application/x-www-form-urlencoded\">"
|
||||
" <table>"
|
||||
" <tr>"
|
||||
" <td><label for=\"name\">%s: </label></td>"
|
||||
" <td><input type=\"text\" name=\"name\" value=\"%s\" required></td>"
|
||||
" </tr>"
|
||||
" <tr>"
|
||||
" <td><label for=\"description\">%s: </label></td>"
|
||||
" <td><input type=\"text\" name=\"description\" value=\"%s\" ></td>"
|
||||
" </tr>"
|
||||
" </table>"
|
||||
" <input type=\"submit\" value=\"%s\">"
|
||||
"</form>",
|
||||
I18N("Edit a Room"),
|
||||
id,
|
||||
I18N("Room Name"),
|
||||
name?name:"",
|
||||
I18N("Description"),
|
||||
descr?descr:"",
|
||||
I18N("Submit"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *_handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
{
|
||||
const char *protocol;
|
||||
@@ -325,6 +430,34 @@ GWEN_MSG *_handlePostAdd(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *_handlePostEdit(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id)
|
||||
{
|
||||
const char *protocol;
|
||||
AQH_SERVICE *sv;
|
||||
uint32_t perms;
|
||||
|
||||
protocol=AQH_HttpRequest_GetProtocol(rq);
|
||||
sv=AQH_HttpUrlHandler_GetHttpService(uh);
|
||||
perms=AQH_HttpRequest_GetModulePerms(rq);
|
||||
if (perms & AQHOME_HTTP_PERMS_ADD_ROOM) {
|
||||
AQH_STORAGE *sto;
|
||||
|
||||
sto=AqHomeHttpService_GetStorage(sv);
|
||||
if (sto) {
|
||||
return _editRoomCreateResponse(uh, rq, id);
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(NULL, "No storage");
|
||||
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return AQH_HttpService_CreateResponseMsg(sv, 403, "Forbidden", protocol, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *_addRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
{
|
||||
AQH_SERVICE *sv;
|
||||
@@ -382,4 +515,62 @@ GWEN_MSG *_addRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq)
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *_editRoomCreateResponse(AQH_HTTP_URLHANDLER *uh, AQH_HTTP_REQUEST *rq, int id)
|
||||
{
|
||||
AQH_SERVICE *sv;
|
||||
const char *protocol;
|
||||
GWEN_DB_NODE *db;
|
||||
const char *roomName;
|
||||
const char *roomDescr;
|
||||
AQH_STORAGE *sto;
|
||||
|
||||
sv=AQH_HttpUrlHandler_GetHttpService(uh);
|
||||
protocol=AQH_HttpRequest_GetProtocol(rq);
|
||||
db=AQH_HttpRequest_GetDbPostBody(rq);
|
||||
|
||||
roomName=GWEN_DB_GetCharValue(db, "name", 0, NULL);
|
||||
roomDescr=GWEN_DB_GetCharValue(db, "description", 0, NULL);
|
||||
|
||||
if (!(roomName && *roomName)) {
|
||||
DBG_INFO(NULL, "Missing room name");
|
||||
return AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Missing room name"), 1, db, _writeAddPage);
|
||||
}
|
||||
|
||||
sto=AqHomeHttpService_GetStorage(sv);
|
||||
if (sto) {
|
||||
AQH_ROOM *r;
|
||||
int rv;
|
||||
|
||||
rv=AqHomeHttpService_LockStorage(sv);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error locking storage");
|
||||
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
|
||||
}
|
||||
|
||||
r=AQH_Storage_GetRoomById(sto, id);
|
||||
if (r==NULL) {
|
||||
AqHomeHttpService_UnlockStorage(sv);
|
||||
DBG_ERROR(NULL, "Room %d not found", id);
|
||||
return AQH_HttpUrlHandler_CreatePageMessage(uh, rq, "red", I18N("Room not found"), 1, db, _writeEditPage);
|
||||
}
|
||||
|
||||
/* edit room */
|
||||
AQH_Room_SetName(r, roomName);
|
||||
if (roomDescr && *roomDescr)
|
||||
AQH_Room_SetDescription(r, roomDescr);
|
||||
AQH_Storage_AddRuntimeFlags(sto, AQH_STORAGE_RTFLAGS_MODIFIED);
|
||||
|
||||
rv=AqHomeHttpService_UnlockStorage(sv);
|
||||
if (rv<0) {
|
||||
DBG_ERROR(NULL, "Error unlocking storage");
|
||||
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
|
||||
}
|
||||
return AQH_HttpService_CreateRedirectingResponseMsg(sv, protocol, "/rooms/list");
|
||||
}
|
||||
else
|
||||
return AQH_HttpService_CreateResponseMsg(sv, 500, "Internal error", protocol, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user