Esempio n. 1
0
// Translate section
func (controller *SectionController) AddTranslation(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		err    error
		exists bool
		data   map[string]string
	)

	vars := mux.Vars(r)

	if exists, err = model.GetSectionManager(dbMap).Exists(vars["entity_id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if exists == false {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if _, ok := data["name"]; !ok {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if err = model.GetSectionManager(dbMap).AddTranslation(vars["entity_id"], vars["child_id"], data["name"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("AddTranslationError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("AddTranslationSuccess"), Empty{})
}
Esempio n. 2
0
// Remove competition
func (controller *SectionController) RemoveCompetition(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var err error
	vars := mux.Vars(r)
	if err = model.GetSectionManager(dbMap).RemoveCompetition(vars["entity_id"], vars["child_id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("RemoveCompetitionError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("RemoveCompetitionSuccess"), Empty{})
}
Esempio n. 3
0
// Update section
func (controller *SectionController) Update(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {

	var (
		data   map[string]interface{}
		exists bool
		entity *model.Section
		err    error
	)

	if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if isValid := controller.ValidateData(data); !isValid {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	vars := mux.Vars(r)

	if exists, err = model.GetSectionManager(dbMap).Exists(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if exists == false {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	if err = model.GetSectionManager(dbMap).UpdateRecord(data); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("UpdateError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if entity, err = model.GetSectionManager(dbMap).Read(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("UpdateSuccess"), controller.GetResponseData(entity))
}
Esempio n. 4
0
// List sections
func (controller *SectionController) List(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		err         error
		sectionList *[]model.Section
	)
	if sectionList, err = model.GetSectionManager(dbMap).List(); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details:records %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ListError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("ListSuccess"), sectionList)
}
Esempio n. 5
0
// Read section
func (controller *SectionController) Read(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		entity *model.Section
		err    error
	)
	vars := mux.Vars(r)
	if entity, err = model.GetSectionManager(dbMap).Read(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}
	if entity == nil {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("ReadSuccess"), controller.GetResponseData(entity))
}
Esempio n. 6
0
// Create section
func (controller *SectionController) Create(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		entity model.Section
		err    error
	)
	if err = json.NewDecoder(r.Body).Decode(&entity); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}
	if err = entity.IsValid(); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("ValidationError"))
	}
	if err = model.GetSectionManager(dbMap).Create(&entity); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("CreateError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("CreateSuccess"), controller.GetResponseData(&entity))
}