<commit_before>// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package web

import (
	"reflect"
)

//Registered controllers
var controllers map[string]map[string]reflect.Value = make(map[string]map[string]reflect.Value)

//controller struct
type Controller struct {
	//Request
	Request *Request
}

// New router register
func NewController(module string, c interface{}) {
	if _, ok := controllers[module]; !ok {
		controllers[module] = make(map[string]reflect.Value)
	}

	value := reflect.ValueOf(c)
	controllers[module][value.Elem().Type().Name()] = value
}<commit_msg>restore controller<commit_after>// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package web

import (
	"encoding/json"
	"github.com/zhgo/console"
	"github.com/zhgo/db"
	"reflect"
)

//Registered controllers
var controllers map[string]map[string]reflect.Value = make(map[string]map[string]reflect.Value)

//controller struct
type Controller struct {
	//Request
	Request *Request
}

// New router register
func NewController(module string, c interface{}) {
	if _, ok := controllers[module]; !ok {
		controllers[module] = make(map[string]reflect.Value)
	}

	value := reflect.ValueOf(c)
	controllers[module][value.Elem().Type().Name()] = value
}

// Add
func (c *Controller) Add() interface{} {
	return c.crud(func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{} {
		r, err := s.InsertInto(tbName).Exec(jsonPost)
		if err != nil {
			return Result{-600, err.Error()}
		}

		return Result{r.LastInsertId, ""}
	})
}

// Detail
func (c *Controller) Detail(key string) interface{} {
	return c.crud(func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{} {
		d := db.Item{}
		q := s.NewQuery()
		err := q.Select("*").From(tbName).Where(q.Eq(primary, key)).Row(&d)
		if err != nil {
			return Result{-600, err.Error()}
		}

		return Result{d, ""}
	})
}

// Edit
func (c *Controller) Edit(key string) interface{} {
	return c.crud(func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{} {
		w := db.Where{primary: key}
		r, err := s.Update(tbName).Exec(jsonPost, w)
		if err != nil {
			return Result{-600, err.Error()}
		}

		return Result{r.RowsAffected, ""}
	})
}

// Delete
func (c *Controller) Delete(key string) interface{} {
	return c.crud(func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{} {
		w := db.Where{primary: key}
		r, err := s.DeleteFrom(tbName).Exec(w)
		if err != nil {
			return Result{-600, err.Error()}
		}

		return Result{r.RowsAffected, ""}
	})
}

// List
func (c *Controller) List(pi, ps int64) interface{} {
	return c.crud(func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{} {
		d := []db.Item{}
		q := s.NewQuery()
		err := q.Select("*").From(tbName).Rows(&d)
		if err != nil {
			return Result{-600, err.Error()}
		}

		return Result{d, ""}
	})
}

// crud
func (c *Controller) crud(fn func(jsonPost db.Item, s *db.Server, tbName string, primary string) interface{}) interface{} {
	jsonPost := db.Item{}
	err := json.NewDecoder(c.Request.HTTPRequest.Body).Decode(&jsonPost)
	if err != nil {
		return Result{-100, err.Error()}
	}

	if s, ok := db.Servers[App.Modules[c.Request.Module].DB.Name]; ok {
		tbName := console.CamelcaseToUnderscore(c.Request.Module) + "_" + console.CamelcaseToUnderscore(c.Request.Controller)
		primary := tbName + "_id"
		return fn(jsonPost, s, tbName, primary)
	} else {
		return Result{-101, "No database configuration"}
	}
}
{'fraction_non_alphanumeric': 0.16027027027027027, 'fraction_numerical': 0.008378378378378378, 'mean_word_length': 4.1402777777777775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '450784', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1259, 'n_words': 333}