/
characs.go
1245 lines (1096 loc) · 32.6 KB
/
characs.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ArkeoGIS - The Geographic Information System for Archaeologists
* Copyright (C) 2015-2016 CROLL SAS
*
* Authors :
* Nicolas Dimitrijevic <nicolas@croll.fr>
* Christophe Beveraggi <beve@croll.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package rest
import (
"encoding/csv"
"encoding/json"
"fmt"
"log"
"reflect"
"strconv"
"net/http"
db "github.com/croll/arkeogis-server/db"
"github.com/croll/arkeogis-server/model"
"github.com/croll/arkeogis-server/translate"
routes "github.com/croll/arkeogis-server/webserver/routes"
"github.com/jmoiron/sqlx"
sqlx_types "github.com/jmoiron/sqlx/types"
)
type CharacGetParams struct {
Id int `min:"1" error:"Charac Id is mandatory"`
Project_id int
}
type CharacRootsParams struct {
Project_id int
}
type CharacListCsvParams struct {
Id int `min:"1" error:"Charac Id is mandatory"`
Isocode string `json:"isocode"`
Dl string `json:"dl"`
Html int `json:"html"`
}
type CharacSetHiddensParams struct {
Id int `min:"1" error:"Charac Id is mandatory"`
Project_id int
}
func init() {
Routes := []*routes.Route{
&routes.Route{
Path: "/api/characs/flat",
Func: CharacsAll,
Description: "Get all characs in all languages",
Method: "GET",
},
&routes.Route{
Path: "/api/characs",
Func: CharacsRoots,
Description: "Get all root characs in all languages",
Method: "GET",
Params: reflect.TypeOf(CharacRootsParams{}),
},
&routes.Route{
Path: "/api/characs/csv",
Func: CharacListCsv,
Description: "Get all characs as csv",
Params: reflect.TypeOf(CharacListCsvParams{}),
Method: "GET",
Permissions: []string{},
},
&routes.Route{
Path: "/api/characs",
Description: "Create/Update a charac",
Func: CharacsUpdate,
Method: "POST",
Json: reflect.TypeOf(CharacsUpdateStruct{}),
Permissions: []string{
"request map",
},
},
&routes.Route{
Path: "/api/characs/{id:[0-9]+}",
Func: CharacsGetTree,
Description: "Get a charac in all languages",
Method: "GET",
Params: reflect.TypeOf(CharacGetParams{}),
},
&routes.Route{
Path: "/api/characs/{id:[0-9]+}",
Description: "Delete a charac",
Func: CharacsDelete,
Method: "DELETE",
Permissions: []string{
"adminusers",
},
Params: reflect.TypeOf(CharacGetParams{}),
},
&routes.Route{
Path: "/api/characs/{id:[0-9]+}/hiddens/{project_id:[0-9]+}",
Description: "",
Func: CharacSetHiddens,
Method: "POST",
Params: reflect.TypeOf(CharacSetHiddensParams{}),
Json: reflect.TypeOf(CharacSetHiddensStruct{}),
Permissions: []string{
"request map",
},
},
}
routes.RegisterMultiple(Routes)
}
func CharacsAll(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
type row struct {
Parent_id int `db:"parent_id" json:"parent_id"`
Id int `db:"id" json:"id"`
Tr sqlx_types.JSONText `db:"tr" json:"tr"`
}
characs := []row{}
//err := db.DB.Select(&characs, "select parent_id, id, to_json((select array_agg(charac_tr.*) from charac_tr where charac_tr.charac_id = charac.id)) as tr FROM charac order by parent_id, \"order\", id")
transquery := model.GetQueryTranslationsAsJSONObject("charac_tr", "tbl.charac_id = charac.id", "", false, "name")
q := "select parent_id, id, (" + transquery + ") as tr FROM charac order by parent_id, \"order\", id"
fmt.Println("q: ", q)
err := db.DB.Select(&characs, q)
fmt.Println("characs: ", characs)
if err != nil {
fmt.Println("err: ", err)
return
}
j, err := json.Marshal(characs)
w.Write(j)
}
// CharacsRoots write all root characs in all languages
func CharacsRoots(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
type row struct {
model.Charac_root
model.Charac
Name map[string]string `json:"name"`
Description map[string]string `json:"description"`
HiddensCount int `json:"hiddens_count"`
//UsersInGroup []model.User `json:"users_in_group" ignore:"true"` // read-only, used to display users of the group
//Author model.User `json:"author" ignore:"true"` // read-only, used to display users of the group
}
params := proute.Params.(*CharacRootsParams)
characs := []*row{}
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
log.Println(err)
userSqlError(w, err)
return
}
// if Project_id is specified, verify that we are the owner of the project
if params.Project_id != 0 {
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsUpdate: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsUpdate: can't cast user...", _user)
_ = tx.Rollback()
return
}
// check if the user is the owner of the project
count := 0
tx.Get(&count, "SELECT count(*) FROM project WHERE id = "+strconv.Itoa(params.Project_id)+" AND user_id = "+strconv.Itoa(user.Id))
if count != 1 {
log.Println("CharacSetHiddens: user is not the owner...", user, params.Project_id)
_ = tx.Rollback()
return
}
}
// load all roots
err = db.DB.Select(&characs, "SELECT charac_root.* FROM charac_root LEFT JOIN charac on charac_root.root_charac_id = charac.id ORDER BY charac.order")
if err != nil {
log.Println(err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// load all root characs
for _, charac := range characs {
charac.Charac.Id = charac.Charac_root.Root_charac_id
err = charac.Charac.Get(tx)
if err != nil {
log.Println(err)
_ = tx.Rollback()
userSqlError(w, err)
return
}
// load translations
tr := []model.Charac_tr{}
err = tx.Select(&tr, "SELECT * FROM charac_tr WHERE charac_id = "+strconv.Itoa(charac.Charac.Id))
if err != nil {
log.Println(err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
charac.Name = model.MapSqlTranslations(tr, "Lang_isocode", "Name")
charac.Description = model.MapSqlTranslations(tr, "Lang_isocode", "Description")
// load custom modified characs
// get the author user
/* err = charac.Author.Get(tx)
charac.Author.Password = ""
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
*/
if params.Project_id > 0 {
hidden_count := 0
err = tx.Get(&hidden_count, `WITH RECURSIVE subcharac(id, parent_id, charac_id, project_id) AS (
SELECT id, parent_id, phc.charac_id, phc.project_id
FROM charac LEFT JOIN project_hidden_characs phc ON phc.charac_id = charac.id WHERE id = $1
UNION ALL
SELECT c2.id, c2.parent_id, phc2.charac_id, phc2.project_id
FROM subcharac AS sc, charac AS c2 LEFT JOIN project_hidden_characs phc2 ON phc2.charac_id = c2.id
WHERE c2.parent_id = sc.id
)
SELECT count(*) FROM subcharac WHERE project_id=$2`, charac.Id, params.Project_id)
if err != nil {
log.Println(err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
charac.HiddensCount = hidden_count
}
}
// commit...
err = tx.Commit()
if err != nil {
log.Println("commit failed")
userSqlError(w, err)
return
}
j, err := json.Marshal(characs)
w.Write(j)
}
type CharacTreeStructCounts struct {
Name string `json:"name"`
UsageCount int `json:"usagecount"`
}
type CharacTreeStruct struct {
model.Charac
Name map[string]string `json:"name"`
Description map[string]string `json:"description"`
Content []CharacTreeStruct `json:"content"`
Hidden bool `json:"hidden"`
UsageCount int `json:"usageCount"`
Usages []CharacTreeStructCounts `json:"usages"`
}
// CharacsUpdateStruct structure (json)
type CharacsUpdateStruct struct {
model.Charac_root
CharacTreeStruct
UsersInGroup []model.User `json:"users_in_group" ignore:"true"` // read-only, used to display users of the group
Author model.User `json:"author" ignore:"true"` // read-only, used to display users of the group
}
// update charac recursively
func setCharacRecursive(tx *sqlx.Tx, charac *CharacTreeStruct, parent *CharacTreeStruct) error {
var err error = nil
// if we are the root, we have no parent id
if parent != nil {
charac.Parent_id = parent.Id
} else {
charac.Parent_id = 0
}
// save charac...
if charac.Id > 0 {
err = charac.Update(tx)
if err != nil {
return err
}
} else {
err = charac.Create(tx)
if err != nil {
return err
}
}
//log.Println("c: ", charac)
// delete any translations
_, err = tx.Exec("DELETE FROM charac_tr WHERE charac_id = $1", charac.Id)
if err != nil {
return err
}
// create a map of translations for name...
tr := map[string]*model.Charac_tr{}
for isocode, name := range charac.Name {
tr[isocode] = &model.Charac_tr{
Charac_id: charac.Id,
Lang_isocode: isocode,
Name: name,
}
}
// continue to update this map with descriptions...
for isocode, description := range charac.Description {
m, ok := tr[isocode]
if ok {
m.Description = description
} else {
tr[isocode] = &model.Charac_tr{
Charac_id: charac.Id,
Lang_isocode: isocode,
Description: description,
}
}
}
// now insert translations rows in database...
for _, m := range tr {
err = m.Create(tx)
if err != nil {
return err
}
}
// recursively call to subcontents...
ids := []int{} // this array will be usefull to delete others charac of this sub level that does not exists anymore
for _, sub := range charac.Content {
err = setCharacRecursive(tx, &sub, charac)
if err != nil {
return err
}
ids = append(ids, sub.Charac.Id)
}
// search any charac that should be deleted
ids_to_delete := []int{} // the array of characs id to delete
err = tx.Select(&ids_to_delete, "SELECT id FROM charac WHERE id NOT IN ("+model.IntJoin(ids, true)+") AND parent_id = "+strconv.Itoa(charac.Charac.Id))
if err != nil {
return err
}
// delete translations of the characs that should be deleted
_, err = tx.Exec("DELETE FROM charac_tr WHERE charac_id IN (" + model.IntJoin(ids_to_delete, true) + ")")
if err != nil {
return err
}
// delete hiddens characs from project, if any
_, err = tx.Exec("DELETE FROM project_hidden_characs WHERE charac_id IN (" + model.IntJoin(ids_to_delete, true) + ")")
if err != nil {
return err
}
// delete characs itselfs...
_, err = tx.Exec("DELETE FROM charac WHERE id IN (" + model.IntJoin(ids_to_delete, true) + ")")
if err != nil {
return err
}
return err
}
// CharacsUpdate Create/Update a charac
func CharacsUpdate(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
// get the post
c := proute.Json.(*CharacsUpdateStruct)
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
userSqlError(w, err)
return
}
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsUpdate: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsUpdate: can't cast user...", _user)
_ = tx.Rollback()
return
}
err = user.Get(tx)
user.Password = "" // immediatly erase password field, we don't need it
if err != nil {
log.Println("CharacsUpdate: can't load user...", _user)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// boolean create, true if we are creating a totaly new charac
var create bool
if c.Charac.Id > 0 {
create = false
// @TODO: check that you are in group of this charac when updating one
} else {
create = true
}
// save recursively this charac
err = setCharacRecursive(tx, &c.CharacTreeStruct, nil)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
//log.Println("geom: ", c.Charac_root.Geom)
// save the charac_root row, but search/create it's group first
c.Charac_root.Root_charac_id = c.Charac.Id
if create {
// when creating, we also must create it's working group
group := model.Group{
Type: "charac",
}
err = group.Create(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
// also save group name in langs...
for isocode, name := range c.CharacTreeStruct.Name {
group_tr := model.Group_tr{
Group_id: group.Id,
Lang_isocode: isocode,
Name: name,
}
err = group_tr.Create(tx)
}
// create the charac root
c.Charac_root.Admin_group_id = group.Id
err = c.Charac_root.Create(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
} else {
// search the characroot to verify permissions
characroot := model.Charac_root{
Root_charac_id: c.Charac.Id,
}
err = characroot.Get(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
// take the group
group := model.Group{
Id: characroot.Admin_group_id,
}
err = group.Get(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
// update translations of the group
_, err = tx.Exec("DELETE FROM group_tr WHERE group_id = " + strconv.Itoa(group.Id))
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
for isocode, name := range c.CharacTreeStruct.Name {
group_tr := model.Group_tr{
Group_id: group.Id,
Lang_isocode: isocode,
Name: name,
}
err = group_tr.Create(tx)
}
// check that the user is in the group
var ok bool
ok, err = user.HaveGroups(tx, group)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return
}
if !ok {
/*
routes.ServerError(w, 403, "unauthorized")
_ = tx.Rollback()
return
*/
}
// only theses fields can be modified
//characroot.Credits = c.Charac_root.Credits
//characroot.Active = c.Charac_root.Active
//characroot.Geom = c.Charac_root.Geom
//characroot.Author_user_id = c.Charac_root.Author_user_id
//characroot.Admin_group_id = c.Charac_root.Admin_group_id
characroot.Cached_langs = c.Charac_root.Cached_langs
err = characroot.Update(tx)
if err != nil {
log.Println("characroot update failed")
userSqlError(w, err)
_ = tx.Rollback()
return
}
}
answer, err := characsGetTree(w, tx, c.Id, 0, false, user)
// commit...
err = tx.Commit()
if err != nil {
log.Println("commit failed")
userSqlError(w, err)
_ = tx.Rollback()
return
}
j, err := json.Marshal(answer)
if err != nil {
log.Println("marshal failed: ", err)
}
//log.Println("result: ", string(j))
w.Write(j)
}
func getCharacRecursive(tx *sqlx.Tx, charac *CharacTreeStruct, project_id int, getUsageCount bool) error {
var err error = nil
// load translations
tr := []model.Charac_tr{}
err = tx.Select(&tr, "SELECT * FROM charac_tr WHERE charac_id = "+strconv.Itoa(charac.Id))
if err != nil {
return err
}
charac.Name = model.MapSqlTranslations(tr, "Lang_isocode", "Name")
charac.Description = model.MapSqlTranslations(tr, "Lang_isocode", "Description")
// check if enabled in project
if project_id > 0 {
hiddenCount := 0
tx.Get(&hiddenCount, "SELECT count(*) FROM project_hidden_characs WHERE project_id = "+strconv.Itoa(project_id)+" AND charac_id = "+strconv.Itoa(charac.Id))
if hiddenCount > 0 {
charac.Hidden = true
//log.Println("found hidden : ", charac.Id)
}
}
if getUsageCount {
// get count of characs usage
// select count(*) from site_range__charac left join site_range on site_range__charac.site_range_id=site_range.id left join site on site.id=site_range.site_id left join database on site.database_id = database.id where database.published='t' AND site_range__charac.charac_id = 12;
err = tx.Get(&charac.UsageCount, "select count(*) from site_range__charac left join site_range on site_range__charac.site_range_id=site_range.id left join site on site.id=site_range.site_id left join database on site.database_id = database.id where database.published='t' AND site_range__charac.charac_id = "+strconv.Itoa(charac.Id))
if err != nil {
return err
}
err = tx.Select(&charac.Usages, "select database.name,count(site_range__charac.id) as usagecount from site_range__charac left join site_range on site_range__charac.site_range_id=site_range.id left join site on site.id=site_range.site_id left join database on site.database_id = database.id where database.published='t' AND site_range__charac.charac_id = "+strconv.Itoa(charac.Id)+" GROUP BY database.id order by usagecount desc")
if err != nil {
return err
}
// pour evolution, avoir les bases ;
// select count(*) from site_range__charac left join site_range on site_range__charac.site_range_id=site_range.id left join site on site.id=site_range.site_id left join database on site.database_id = database.id where database.published='t' AND site_range__charac.charac_id = 12
//
// les bases avec le nombre d'utilisation par base :
// select database.name,count(site_range__charac.id) from site_range__charac left join site_range on site_range__charac.site_range_id=site_range.id left join site on site.id=site_range.site_id left join database on site.database_id = database.id where database.published='t' AND site_range__charac.charac_id = 12 GROUP BY database.id order by count desc;
}
// get the childs of this charac from the db
childs, err := charac.Charac.Childs(tx)
if err != nil {
return err
}
// recurse
charac.Content = make([]CharacTreeStruct, len(childs))
for i, child := range childs {
charac.Content[i].Charac = child
err = getCharacRecursive(tx, &charac.Content[i], project_id, getUsageCount)
if err != nil {
return err
}
}
return nil
}
func characsGetTree(w http.ResponseWriter, tx *sqlx.Tx, id int, project_id int, getUsageCount bool, user model.User) (answer *CharacsUpdateStruct, err error) {
// answer structure that will be printed when everything is done
answer = &CharacsUpdateStruct{}
// get the charac_root row
answer.Charac_root.Root_charac_id = id
err = answer.Charac_root.Get(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}
// get the charac (root)
answer.Charac.Id = id
err = answer.Charac.Get(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}
// now get the charac translations and all childrens
err = getCharacRecursive(tx, &answer.CharacTreeStruct, project_id, getUsageCount)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}
// get users of the charac group
group := model.Group{
Id: answer.Charac_root.Admin_group_id,
}
err = group.Get(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}
answer.UsersInGroup, err = group.GetUsers(tx)
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}
for i := range answer.UsersInGroup {
answer.UsersInGroup[i].Password = ""
}
// get the author user
/* answer.Author.Id = answer.Charac_root.Author_user_id
err = answer.Author.Get(tx)
answer.Author.Password = ""
if err != nil {
userSqlError(w, err)
_ = tx.Rollback()
return nil, err
}*/ // no author in characs vs chrono
return answer, nil
}
func CharacsGetTree(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
params := proute.Params.(*CharacGetParams)
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
userSqlError(w, err)
return
}
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsGetTree: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsGetTree: can't cast user...", _user)
_ = tx.Rollback()
return
}
err = user.Get(tx)
user.Password = "" // immediatly erase password field, we don't need it
if err != nil {
log.Println("CharacsGetTree: can't load user...", _user)
userSqlError(w, err)
_ = tx.Rollback()
return
}
answer, err := characsGetTree(w, tx, params.Id, params.Project_id, false, user)
// commit...
err = tx.Commit()
if err != nil {
log.Println("commit failed")
userSqlError(w, err)
_ = tx.Rollback()
return
}
j, err := json.Marshal(answer)
if err != nil {
log.Println("marshal failed: ", err)
}
//log.Println("result: ", string(j))
w.Write(j)
}
func characsDeleteRecurse(charac CharacTreeStruct, tx *sqlx.Tx) error {
var err error
for _, charac := range charac.Content {
err = characsDeleteRecurse(charac, tx)
if err != nil {
return err
}
}
_, err = tx.Exec("DELETE FROM charac_tr WHERE charac_id = " + strconv.Itoa(charac.Id))
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM charac WHERE id = " + strconv.Itoa(charac.Id))
if err != nil {
return err
}
return nil
}
func CharacsDelete(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
params := proute.Params.(*CharacGetParams)
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
userSqlError(w, err)
return
}
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsUpdate: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsUpdate: can't cast user...", _user)
_ = tx.Rollback()
return
}
err = user.Get(tx)
user.Password = "" // immediatly erase password field, we don't need it
if err != nil {
log.Println("CharacsUpdate: can't load user...", _user)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// get the full characlogie tree
answer, err := characsGetTree(w, tx, params.Id, 0, false, user)
// delete charac_root
err = answer.Charac_root.Delete(tx)
if err != nil {
log.Println("delete Charac root", err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// delete admin gruop in user__group...
_, err = tx.Exec("DELETE FROM user__group WHERE group_id = " + strconv.Itoa(answer.Admin_group_id))
if err != nil {
log.Println("delete admin users group failed", err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// delete admin gruop in group_tr...
_, err = tx.Exec("DELETE FROM \"group_tr\" WHERE group_id = " + strconv.Itoa(answer.Admin_group_id))
if err != nil {
log.Println("delete admin group_tr failed", err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// delete admin gruop in group...
_, err = tx.Exec("DELETE FROM \"group\" WHERE id = " + strconv.Itoa(answer.Admin_group_id))
if err != nil {
log.Println("delete admin group failed", err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// recursively delete charac...
err = characsDeleteRecurse(answer.CharacTreeStruct, tx)
if err != nil {
log.Println("characsDeleteRecurse failed", err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
// commit...
err = tx.Commit()
if err != nil {
log.Println("commit failed")
userSqlError(w, err)
_ = tx.Rollback()
return
}
}
// CharacSetHiddensStruct structure (json)
type CharacSetHiddensStruct struct {
HiddenIds []int `json:"hidden_ids"`
}
func CharacSetHiddens(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
params := proute.Params.(*CharacSetHiddensParams)
c := proute.Json.(*CharacSetHiddensStruct)
log.Println("c: ", c)
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
userSqlError(w, err)
return
}
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsUpdate: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsUpdate: can't cast user...", _user)
_ = tx.Rollback()
return
}
// check if the user is the owner of the project
count := 0
tx.Get(&count, "SELECT count(*) FROM project WHERE id = "+strconv.Itoa(params.Project_id)+" AND user_id = "+strconv.Itoa(user.Id))
if count != 1 {
log.Println("CharacSetHiddens: user is not the owner...", user, params.Project_id)
_ = tx.Rollback()
return
}
// delete previous settings
_, err = tx.Exec(`DELETE FROM project_hidden_characs WHERE charac_id in (
WITH RECURSIVE subcharac(id, parent_id, charac_id, project_id) AS (
SELECT id, parent_id, phc.charac_id, phc.project_id
FROM charac LEFT JOIN project_hidden_characs phc ON phc.charac_id = charac.id WHERE id = $1
UNION ALL
SELECT c2.id, c2.parent_id, phc2.charac_id, phc2.project_id
FROM subcharac AS sc, charac AS c2 LEFT JOIN project_hidden_characs phc2 ON phc2.charac_id = c2.id
WHERE c2.parent_id = sc.id
)
SELECT id FROM subcharac WHERE project_id=$2)`, params.Id, params.Project_id)
if err != nil {
log.Println(err)
userSqlError(w, err)
_ = tx.Rollback()
return
}
for _, id := range c.HiddenIds {
log.Println("deleting: ", id)
tx.Exec("INSERT INTO project_hidden_characs (project_id, charac_id) VALUES (" + strconv.Itoa(params.Project_id) + "," + strconv.Itoa(id) + ")")
}
// commit...
err = tx.Commit()
if err != nil {
log.Println("commit failed")
userSqlError(w, err)
_ = tx.Rollback()
return
}
}
func usagesToString(usages *[]CharacTreeStructCounts) string {
res := "";
for _, usage := range *usages {
if res != "" {
res = res + ", "
}
res = res + usage.Name + " (" + strconv.Itoa(usage.UsageCount) + ")"
}
return res
}
func CharacListCsv(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
params := proute.Params.(*CharacListCsvParams)
// transaction begin...
tx, err := db.DB.Beginx()
if err != nil {
userSqlError(w, err)
return
}
// get the user
_user, ok := proute.Session.Get("user")
if !ok {
log.Println("CharacsUpdate: can't get user in session...", _user)
_ = tx.Rollback()
return
}
user, ok := _user.(model.User)
if !ok {
log.Println("CharacsUpdate: can't cast user...", _user)
_ = tx.Rollback()
return
}
getUsageCount := false
if params.Html == 1 {
getUsageCount = true
}
answer, err := characsGetTree(w, tx, params.Id, 0, getUsageCount, user)
if err != nil {
userSqlError(w, err)
return
}
table := [][]string{}
table = append(table, []string{
"IDArkeoGIS",
"CARAC_NAME",
"CARAC_LVL1",