Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Будет удалено .idea/
Будет удалено vendor/
/vendor/
.idea
.idea
/*.db
Будет удалено test.db
55 changes: 36 additions & 19 deletions checking/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
package checking

import (
"HotKeysBackend/key"
"HotKeysBackend/hotkey"
"HotKeysBackend/utils"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)

type CheckHotkeyRequest struct {
ProgramId uint `json:"programId"`
HotkeyId uint `json:"hotkeyId"`
Combination []*key.Key `json:"combination"`
}

func HandleCheckHotkey(context *gin.Context, checker Service) {
paramProgramId, err := strconv.ParseUint(context.Param(utils.ProgramId), 10, 64)
programId := getProgramId(context)
hotkeyId := getHotkeyId(context)
combination := make([]uint, hotkey.MAX_LENGTH)

if context.Request.Body == nil {
utils.SendError(context, nil)
panic(utils.ErrorWrongBodyFormat)
}

buf := make([]byte, 1024)
num, err := context.Request.Body.Read(buf)
if err != nil {
utils.SendError(context, err)
panic(utils.ErrorWrongProgramIdFormat)
panic(utils.ErrorWrongBodyFormat)
}
programId := uint(paramProgramId)

paramHotkeyId, err := strconv.ParseUint(context.Param(utils.HotkeyId), 10, 64)
reqBody := string(buf[0:num])
context.JSON(http.StatusOK, reqBody)

right, err := checker.Check(programId, hotkeyId, combination)
if err != nil {
utils.SendError(context, err)
panic(utils.ErrorWrongHotkeyIdFormat)
panic(utils.ErrorChecking)
}
hotkeyId := uint(paramHotkeyId)

checkHotkeyRequest := &CheckHotkeyRequest{
ProgramId: programId,
HotkeyId: hotkeyId,
//Combination:
if right {
context.JSON(http.StatusOK, gin.H{"answer": true})
} else {
context.JSON(http.StatusOK, gin.H{"answer": false})
}
}

err = context.BindJSON(checkHotkeyRequest)
func getProgramId(context *gin.Context) uint {
paramProgramId, err := strconv.ParseUint(context.Param(utils.ProgramId), 10, 64)
if err != nil {
utils.SendError(context, err)
panic(utils.ErrorWrongBodyFormat)
panic(utils.ErrorWrongProgramIdFormat)
}
return uint(paramProgramId)
}

func getHotkeyId(context *gin.Context) uint {
paramHotkeyId, err := strconv.ParseUint(context.Param(utils.HotkeyId), 10, 64)
if err != nil {
utils.SendError(context, err)
panic(utils.ErrorWrongHotkeyIdFormat)
}
return uint(paramHotkeyId)
}
6 changes: 3 additions & 3 deletions checking/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Service interface {
Check(projectId uint, hotkeyId uint, combination []*key.Key) (bool, error)
Check(projectId uint, hotkeyId uint, combination []uint) (bool, error)
}

type service struct {
Expand All @@ -23,7 +23,7 @@ func CreateService(programRepository program.Repository, keyRepository key.Repos
}

// TODO
func (s *service) Check(projectId uint, hotkeyId uint, combination []*key.Key) (bool, error) {
func (s *service) Check(projectId uint, hotkeyId uint, combination []uint) (bool, error) {
currentHotkeys, err := s.programRepository.GetHotkeys(projectId)
if err != nil {
return false, utils.ErrorGetProgram
Expand All @@ -32,7 +32,7 @@ func (s *service) Check(projectId uint, hotkeyId uint, combination []*key.Key) (
for _, element := range *currentHotkeys {
isFound := true
for i, currentCombination := range *element.Combination {
if combination[i].ID != currentCombination.ID {
if combination[i] != currentCombination.ID {
isFound = false
}
}
Expand Down
2 changes: 2 additions & 0 deletions hotkey/hotkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ type Hotkey struct {
Description string `json:"desc"`
Combination *[]key.Key `json:"combination"`
}

const MAX_LENGTH = 7
26 changes: 22 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import (
"HotKeysBackend/utils"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)

var (
handleGetPrograms = "/programs/"
handleGetProgram = fmt.Sprintf("/programs/:%s", utils.ProgramId)
handleGetHotkeys = fmt.Sprintf("/programs/:%s/hotkeys", utils.ProgramId)
handleCheckHotkey = fmt.Sprintf("/programs/:%s/hotkeys/:%s/check", utils.ProgramId, utils.HotkeyId)
handleGetProgram = fmt.Sprintf("/program/:%s", utils.ProgramId)
handleGetHotkeys = fmt.Sprintf("/program/:%s/hotkeys", utils.ProgramId)
handleCheckHotkey = fmt.Sprintf("/program/:%s/hotkeys/:%s/check", utils.ProgramId, utils.HotkeyId)
)

func main() {
// TODO uncomment when database appears
//db, err := gorm.Open("sqlite3", "test.db")
//if err != nil {
// panic("failed to connect database")
//}
//defer db.Close()
//
//db.Create(&program.Program{})
//programStorage := storage.GetProgramDatabaseRepository()
//keyStorage := storage.GetKeyDatabaseRepository()

Expand All @@ -27,6 +34,17 @@ func main() {
checker := checking.CreateService(programStorage, keyStorage)

router := gin.Default()
//router.Use(cors.New(cors.Config{
// AllowOrigins: []string{"https://foo.com"},
// AllowMethods: []string{"POST", "GET", "PUT"},
// AllowHeaders: []string{"Origin"},
// ExposeHeaders: []string{"Content-Length"},
// AllowCredentials: true,
// AllowOriginFunc: func(origin string) bool {
// return origin == "https://github.com"
// },
// MaxAge: 4 * time.Hour,
//}))

router.GET(handleGetPrograms, func(context *gin.Context) {
getting.HandleGetPrograms(context, getter)
Expand Down
2 changes: 0 additions & 2 deletions storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ const (

func GetProgramDatabaseRepository() program.Repository {
// TODO
//database := getDatabase(databasePath)
return nil
}

func GetKeyDatabaseRepository() key.Repository {
// TODO
//database := getDatabase(databasePath)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var ErrorWrongHotkeyIdFormat = errors.New("hotkey id wrong format")

var ErrorKeyNotFound = errors.New("key not found")

var ErrorChecking = errors.New("error while checking")

func SendError(context *gin.Context, err error) {
context.JSON(http.StatusInternalServerError, gin.H{
// TODO is it safe?
Expand Down