/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  successJsonResponse.go
 *     Version:    1.0.0
 *     Date:       2018-01-14
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver 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.
 *
 * kvmstoreserver 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 kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package essentials

import (
	"encoding/json"
	"net/http"
)

//*****************************************************************************
//*
//*      Public Functions
//*
//*****************************************************************************

// SendSuccessJSONAdd Creates a JSON message with the last (current) revision
// number and using its own internal structure and writes the result directly
// into the ResponseWriter
func SendSuccessJSONAdd(writer http.ResponseWriter, statusCode,

	newRevNbr int, customText string) {

	if customText == "" {
		customText = "Operation successfully finished"
	}

	innerAswerStruct := &struct {
		StatusCode        int    `json:"status_code"`
		Description       string `json:"description"`
		NewRevisionNumber int    `json:"new_revision_number"`
	}{
		StatusCode:        statusCode,
		Description:       customText,
		NewRevisionNumber: newRevNbr,
	}

	jsonOutput, err := json.Marshal(innerAswerStruct)
	if err != nil {
		panic("Internal error when sending the positive Add JSON response. Operation has been successful anyway")
	}

	writer.WriteHeader(statusCode)
	writer.Write(jsonOutput)
}

// SendSuccessJSONDel Creates a JSON message with the original key Name and
// using its own internal structure and writes the result directly into
// the ResponseWriter
func SendSuccessJSONDel(writer http.ResponseWriter, statusCode int,

	keyName, customText string) {
	if customText == "" {
		customText = "Operation successfully finished"
	}

	innerAswerStruct := &struct {
		StatusCode  int    `json:"status_code"`
		Description string `json:"description"`
		KeyName     string `json:"key_name"`
	}{
		StatusCode:  statusCode,
		Description: customText,
		KeyName:     keyName,
	}

	jsonOutput, err := json.Marshal(innerAswerStruct)
	if err != nil {
		panic("Internal error when sending the positive Delete JSON response. Operation has been successful anyway")
	}

	writer.WriteHeader(statusCode)
	writer.Write(jsonOutput)
}
