comparison page-search.c @ 79:52029a52a385

pasterd: revert using ktemplate
author David Demelier <markand@malikania.fr>
date Fri, 17 Mar 2023 07:43:20 +0100
parents 9bfe5ce3cc45
children 1ffe2f5a8500
comparison
equal deleted inserted replaced
78:9bfe5ce3cc45 79:52029a52a385
18 18
19 #include <assert.h> 19 #include <assert.h>
20 #include <string.h> 20 #include <string.h>
21 21
22 #include "database.h" 22 #include "database.h"
23 #include "json-util.h"
24 #include "page-index.h" 23 #include "page-index.h"
25 #include "page-search.h" 24 #include "page-search.h"
25 #include "page-status.h"
26 #include "page.h" 26 #include "page.h"
27 #include "paste.h"
27 #include "util.h" 28 #include "util.h"
28 29
29 #include "html/search.h" 30 #include "html/search.h"
30 31
31 #define TITLE "paster -- search" 32 #define TITLE "paster -- search"
32 #define LIMIT 16 33 #define LIMIT 16
33 34
35 enum {
36 KEYWORD_LANGUAGES
37 };
38
39 struct page {
40 struct kreq *req;
41 struct ktemplate template;
42 };
43
44 static const char * const keywords[] = {
45 [KEYWORD_LANGUAGES] = "languages"
46 };
47
48 static int
49 format(size_t keyword, void *data)
50 {
51 struct page *page = data;
52 struct khtmlreq html;
53
54 khtml_open(&html, page->req, 0);
55
56 switch (keyword) {
57 case KEYWORD_LANGUAGES:
58 for (size_t i = 0; i < languagesz; ++i) {
59 khtml_attr(&html, KELEM_OPTION, KATTR_NAME, languages[i], KATTR__MAX);
60 khtml_puts(&html, languages[i]);
61 khtml_closeelem(&html, 1);
62 }
63 break;
64 default:
65 break;
66 }
67
68 khtml_close(&html);
69
70 return 1;
71 }
72
34 static void 73 static void
35 get(struct kreq *req) 74 get(struct kreq *req)
36 { 75 {
37 page(req, KHTTP_200, html_search, json_pack("{ss so}", 76 struct page self = {
38 "pagetitle", "paster -- search", 77 .req = req,
39 "languages", ju_languages(NULL) 78 .template = {
40 )); 79 .cb = format,
80 .arg = &self,
81 .key = keywords,
82 .keysz = NELEM(keywords)
83 }
84 };
85
86 page(req, KHTTP_200, TITLE, html_search, &self.template);
41 } 87 }
42 88
43 static void 89 static void
44 post(struct kreq *req) 90 post(struct kreq *req)
45 { 91 {
46 json_t *pastes; 92 struct paste pastes[LIMIT];
93 size_t pastesz = NELEM(pastes);
47 const char *key, *val, *title = NULL, *author = NULL, *language = NULL; 94 const char *key, *val, *title = NULL, *author = NULL, *language = NULL;
48 95
49 for (size_t i = 0; i < req->fieldsz; ++i) { 96 for (size_t i = 0; i < req->fieldsz; ++i) {
50 key = req->fields[i].key; 97 key = req->fields[i].key;
51 val = req->fields[i].val; 98 val = req->fields[i].val;
62 if (title && strlen(title) == 0) 109 if (title && strlen(title) == 0)
63 title = NULL; 110 title = NULL;
64 if (author && strlen(author) == 0) 111 if (author && strlen(author) == 0)
65 author = NULL; 112 author = NULL;
66 113
67 if (!(pastes = database_search(16, title, author, language))) 114 if (database_search(pastes, &pastesz, title, author, language) < 0)
68 page_status(req, KHTTP_500); 115 page_status(req, KHTTP_500);
69 else 116 else {
70 page_index_render(req, pastes); 117 page_index_render(req, pastes, pastesz);
118
119 for (size_t i = 0; i < pastesz; ++i)
120 paste_finish(&pastes[i]);
121 }
71 } 122 }
72 123
73 void 124 void
74 page_search(struct kreq *req) 125 page_search(struct kreq *req)
75 { 126 {