<commit_before>package controllers

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/upamune/ShampooServer/models"
	"github.com/zenazn/goji/web"
	"io/ioutil"
	"net/http"
)

const (
	eventBucketID = "EventData"
)

type EventResponse struct {
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
	Major     string `json:"major"`
	Minor     string `json:"minor"`
	Name      string `json:"name"`
	Playing   bool   `json:"Playing"`
	UserID    string `json:"userID"`
	Visit     int    `json:"visit"`
	Id        string `json:"_id"`
	Version        string `json:"_version"`
}

type EventRequest struct {
	Visit int `json:"visit"`
}

type EventController struct {
	kiiApp         models.KiiApp
	eventUrl       string
	eventSearchUrl string
}

func NewEventController(kiiApp models.KiiApp) *EventController {
	eventUrl := "https://api-jp.kii.com/api/apps/" + kiiApp.AppID + "/buckets/" + eventBucketID + "/objects"
	eventSearchUrl := "https://api-jp.kii.com/api/apps/" + kiiApp.AppID + "/buckets/" + eventBucketID + "/query"
	return &EventController{kiiApp: kiiApp, eventUrl: eventUrl, eventSearchUrl: eventSearchUrl}
}

func (ctl *EventController) CreateEvent(c web.C, w http.ResponseWriter, r *http.Request) {
	var eventRawData models.EventRawData

	err := json.NewDecoder(r.Body).Decode(&eventRawData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	eventData := &models.EventData{
		Name:      eventRawData.Name,
		UserID:    eventRawData.UserID,
		Longitude: eventRawData.Longitude,
		Latitude:  eventRawData.Latitude,
		Major:     eventRawData.Major,
		Minor:     eventRawData.Minor,
		Playing:   true,
		Visit:     0,
	}
	json, err := json.Marshal(eventData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req, err := http.NewRequest("POST", ctl.eventUrl, bytes.NewBuffer(json))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	req.Header.Add("Authorization", "Bearer "+eventRawData.AccessToken)
	req.Header.Add("X-Kii-AppID", ctl.kiiApp.AppID)
	req.Header.Add("X-Kii-AppKey", ctl.kiiApp.AppKey)
	req.Header.Add("Content-Type", "application/vnd."+ctl.kiiApp.AppID+".mydata+json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, string(body))
}

func (ctl *EventController) assembleQuery() ([]byte, error) {
	jsonMap := map[string]interface{}{
		"bucketQuery": map[string]interface{}{
			"clause": map[string]interface{}{
				"type":  "eq",
				"field": "Playing",
				"value": true,
			},
			"orderBy":    "_created",
			"descending": true,
		},
	}

	json, err := json.Marshal(jsonMap)
	println(string(json))
	if err != nil {
		return nil, err
	}

	return json, nil
}

func (ctl *EventController) GetEvents(c web.C, w http.ResponseWriter, r *http.Request) {
	accessToken := r.URL.Query().Get("accessToken")
	jsonData, err := ctl.assembleQuery()
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req, err := http.NewRequest("POST", ctl.eventSearchUrl, bytes.NewBuffer(jsonData))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req.Header.Add("Authorization", "Bearer "+accessToken)
	req.Header.Add("Content-Type", "application/vnd.kii.QueryRequest+json")
	req.Header.Add("X-Kii-AppID", ctl.kiiApp.AppID)
	req.Header.Add("X-Kii-AppKey", ctl.kiiApp.AppKey)

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	var data map[string]interface{}
	json.Unmarshal(body, &data)
	results := data["results"]
	jsonData, err = json.Marshal(results)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, string(jsonData))
	return
}

func (ctl *EventController) DeleteEvent(c web.C, w http.ResponseWriter, r *http.Request) {
}

func (ctl *EventController) getEventByBeacon(eventVisitRawData models.EventVisitRawData) ([]byte, error) {
	jsonMap := map[string]interface{}{
		"bucketQuery": map[string]interface{}{
			"clause": map[string]interface{}{
				"type": "and",
				"clauses": []map[string]string{
					{
						"type":  "eq",
						"field": "major",
						"value": eventVisitRawData.Major,
					},
					{
						"type":  "eq",
						"field": "minor",<commit_msg>:rocket: implement delete function<commit_after>package controllers

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/upamune/ShampooServer/models"
	"github.com/zenazn/goji/web"
	"io/ioutil"
	"net/http"
)

const (
	eventBucketID = "EventData"
)

type EventResponse struct {
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
	Major     string `json:"major"`
	Minor     string `json:"minor"`
	Name      string `json:"name"`
	Playing   bool   `json:"Playing"`
	UserID    string `json:"userID"`
	Visit     int    `json:"visit"`
	Id        string `json:"_id"`
	Version        string `json:"_version"`
}

type EventRequest struct {
	Visit int `json:"visit"`
}

type EventController struct {
	kiiApp         models.KiiApp
	eventUrl       string
	eventSearchUrl string
}

func NewEventController(kiiApp models.KiiApp) *EventController {
	eventUrl := "https://api-jp.kii.com/api/apps/" + kiiApp.AppID + "/buckets/" + eventBucketID + "/objects"
	eventSearchUrl := "https://api-jp.kii.com/api/apps/" + kiiApp.AppID + "/buckets/" + eventBucketID + "/query"
	return &EventController{kiiApp: kiiApp, eventUrl: eventUrl, eventSearchUrl: eventSearchUrl}
}

func (ctl *EventController) CreateEvent(c web.C, w http.ResponseWriter, r *http.Request) {
	var eventRawData models.EventRawData

	err := json.NewDecoder(r.Body).Decode(&eventRawData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	eventData := &models.EventData{
		Name:      eventRawData.Name,
		UserID:    eventRawData.UserID,
		Longitude: eventRawData.Longitude,
		Latitude:  eventRawData.Latitude,
		Major:     eventRawData.Major,
		Minor:     eventRawData.Minor,
		Playing:   true,
		Visit:     0,
	}
	json, err := json.Marshal(eventData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req, err := http.NewRequest("POST", ctl.eventUrl, bytes.NewBuffer(json))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	req.Header.Add("Authorization", "Bearer "+eventRawData.AccessToken)
	req.Header.Add("X-Kii-AppID", ctl.kiiApp.AppID)
	req.Header.Add("X-Kii-AppKey", ctl.kiiApp.AppKey)
	req.Header.Add("Content-Type", "application/vnd."+ctl.kiiApp.AppID+".mydata+json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, string(body))
}

func (ctl *EventController) assembleQuery() ([]byte, error) {
	jsonMap := map[string]interface{}{
		"bucketQuery": map[string]interface{}{
			"clause": map[string]interface{}{
				"type":  "eq",
				"field": "Playing",
				"value": true,
			},
			"orderBy":    "_created",
			"descending": true,
		},
	}

	json, err := json.Marshal(jsonMap)
	println(string(json))
	if err != nil {
		return nil, err
	}

	return json, nil
}

func (ctl *EventController) GetEvents(c web.C, w http.ResponseWriter, r *http.Request) {
	accessToken := r.URL.Query().Get("accessToken")
	jsonData, err := ctl.assembleQuery()
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req, err := http.NewRequest("POST", ctl.eventSearchUrl, bytes.NewBuffer(jsonData))
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	req.Header.Add("Authorization", "Bearer "+accessToken)
	req.Header.Add("Content-Type", "application/vnd.kii.QueryRequest+json")
	req.Header.Add("X-Kii-AppID", ctl.kiiApp.AppID)
	req.Header.Add("X-Kii-AppKey", ctl.kiiApp.AppKey)

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	var data map[string]interface{}
	json.Unmarshal(body, &data)
	results := data["results"]
	jsonData, err = json.Marshal(results)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, string(jsonData))
	return
}

func (ctl *EventController) DeleteEvent(c web.C, w http.ResponseWriter, r *http.Request) {
	var eventVisitRawData models.EventVisitRawData
	err := json.NewDecoder(r.Body).Decode(&eventVisitRawData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	ctl.getEventByBeacon(eventVisitRawData)
	jsonData, err := ctl.getEventByBeacon(eventVisitRawData)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	var eventResponse EventResponse

	err = json.NewDecoder(bytes.NewReader(jsonData)).Decode(&eventResponse)
	fmt.Println(eventResponse)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	objectID := eventResponse.Id
	err = ctl.deleteEvent(objectID, eventVisitRawData.AccessToken)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
}

func (ctl *EventController) getEventByBeacon(eventVisitRawData models.EventVisitRawData) ([]byte, error) {
	jsonMap := map[string]interface{}{
		"bucketQuery": map[string]interface{}{
			"clause": map[string]interface{}{
				"type": "and",
				"clauses": []map[string]string{
					{
						"type":  "eq",
						"field": "major",
						"value": eventVisitRawData.Major,
					},
					{
						"type":  "eq",
						"field": "minor",
{'fraction_non_alphanumeric': 0.159948626753606, 'fraction_numerical': 0.00019758940920766647, 'mean_word_length': 4.133367139959432, 'pattern_counts': {'":': 32, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, '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': '2970093', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3663, 'n_words': 724}