generated from VLADIMIR/template
updates
This commit is contained in:
@@ -34,8 +34,39 @@ func mapActionToProtoAction(action *models.Action) *proto.Action {
|
||||
}
|
||||
}
|
||||
|
||||
func mapApplicationToProtoApplication(application *story_service.Application) *proto.Application {
|
||||
func mapStoryApplicationToProtoApplication(application *story_service.Application) *proto.Application {
|
||||
return &proto.Application{
|
||||
Name: application.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func mapStoryApplicationsToApplications(applications []*story_service.Application) []*models.Application {
|
||||
res := make([]*models.Application, 0, len(applications))
|
||||
for _, application := range applications {
|
||||
res = append(res, mapStoryApplicationToApplication(application))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mapStoryApplicationToApplication(application *story_service.Application) *models.Application {
|
||||
return &models.Application{
|
||||
Name: application.Name,
|
||||
State: "NEW",
|
||||
}
|
||||
}
|
||||
|
||||
func mapApplicationsToProtoApplications(applications []*models.Application) []*proto.Application {
|
||||
res := make([]*proto.Application, 0, len(applications))
|
||||
for _, application := range applications {
|
||||
res = append(res, mapApplicationToProtoApplication(application))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func mapApplicationToProtoApplication(application *models.Application) *proto.Application {
|
||||
return &proto.Application{
|
||||
Id: application.ID,
|
||||
Name: application.Name,
|
||||
State: application.State,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,10 @@ func NewRepository() (*Repository, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS applications (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, teamId INTEGER, state TEXT);")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// for tests
|
||||
// _, err = db.Exec("insert into teams (id, name, password) values (1, \"name\", \"pass\");")
|
||||
// if err != nil {
|
||||
@@ -43,12 +47,12 @@ func (r *Repository) GetTeams(ctx context.Context) ([]*models.Team, error) {
|
||||
teams := []*models.Team{}
|
||||
|
||||
for rows.Next() {
|
||||
team := &models.Team{}
|
||||
err := rows.Scan(&team.ID, &team.Name, &team.Password)
|
||||
item := &models.Team{}
|
||||
err := rows.Scan(&item.ID, &item.Name, &item.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
teams = append(teams, team)
|
||||
teams = append(teams, item)
|
||||
}
|
||||
return teams, nil
|
||||
}
|
||||
@@ -76,12 +80,12 @@ func (r *Repository) GetActions(ctx context.Context, teamId int64) ([]*models.Ac
|
||||
actions := []*models.Action{}
|
||||
|
||||
for rows.Next() {
|
||||
team := &models.Action{}
|
||||
err := rows.Scan(&team.ID, &team.Place)
|
||||
item := &models.Action{}
|
||||
err := rows.Scan(&item.ID, &item.Place)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
actions = append(actions, team)
|
||||
actions = append(actions, item)
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
@@ -97,7 +101,7 @@ func (r *Repository) AddActions(ctx context.Context, actions []*models.Action) e
|
||||
}
|
||||
|
||||
func (r *Repository) GetTeam(ctx context.Context, teamId any, password any) (*models.Team, error) {
|
||||
rows, err := r.db.Query("select id, name from teams where id = $1 and password = $2", teamId, password)
|
||||
rows, err := r.db.Query("select id, name from teams where LOWER(name) = LOWER($1) and password = $2", teamId, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -105,15 +109,46 @@ func (r *Repository) GetTeam(ctx context.Context, teamId any, password any) (*mo
|
||||
teams := []*models.Team{}
|
||||
|
||||
for rows.Next() {
|
||||
team := &models.Team{}
|
||||
err := rows.Scan(&team.ID, &team.Name)
|
||||
item := &models.Team{}
|
||||
err := rows.Scan(&item.ID, &item.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
teams = append(teams, team)
|
||||
teams = append(teams, item)
|
||||
}
|
||||
if len(teams) != 1 {
|
||||
return nil, errors.New("bad result")
|
||||
}
|
||||
return teams[0], nil
|
||||
}
|
||||
|
||||
func (r *Repository) AddApplications(ctx context.Context, actions []*models.Action) error {
|
||||
for _, action := range actions {
|
||||
for _, application := range action.Applications {
|
||||
_, err := r.db.Exec("insert into applications (name, teamId, state) values ($1, $2, $3)", application.Name, action.TeamID, application.State)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetApplications(ctx context.Context, teamId int64, state string) ([]*models.Application, error) {
|
||||
rows, err := r.db.Query("select id, name from applications where teamId = $1 and state = $2", teamId, state)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
applications := []*models.Application{}
|
||||
|
||||
for rows.Next() {
|
||||
item := &models.Application{}
|
||||
err := rows.Scan(&item.ID, &item.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
applications = append(applications, item)
|
||||
}
|
||||
return applications, nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"evening_detective/internal/models"
|
||||
"evening_detective/internal/modules/password"
|
||||
"evening_detective/internal/services/story_service"
|
||||
"evening_detective/proto"
|
||||
"strconv"
|
||||
"fmt"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@@ -51,13 +52,18 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro
|
||||
}
|
||||
actions := []*models.Action{
|
||||
{
|
||||
Place: place.Code,
|
||||
TeamID: team.ID,
|
||||
Place: place.Code,
|
||||
TeamID: team.ID,
|
||||
Applications: mapStoryApplicationsToApplications(place.Applications),
|
||||
},
|
||||
}
|
||||
if err := s.repository.AddActions(ctx, actions); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
if err := s.repository.AddApplications(ctx, actions); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
log(team, "add action", actions)
|
||||
return &proto.AddActionRsp{}, nil
|
||||
}
|
||||
|
||||
@@ -86,7 +92,7 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G
|
||||
newAction.Name = place.Name
|
||||
newAction.Applications = make([]*proto.Application, 0, len(place.Applications))
|
||||
for _, application := range place.Applications {
|
||||
newAction.Applications = append(newAction.Applications, mapApplicationToProtoApplication(application))
|
||||
newAction.Applications = append(newAction.Applications, mapStoryApplicationToProtoApplication(application))
|
||||
}
|
||||
res = append(res, newAction)
|
||||
}
|
||||
@@ -104,7 +110,18 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
|
||||
}
|
||||
res := make([]*proto.TeamAdvanced, 0, len(teams))
|
||||
for _, team := range teams {
|
||||
res = append(res, mapTeamsToTeamAdvanced(team))
|
||||
newTeam := mapTeamsToTeamAdvanced(team)
|
||||
actions, err := s.repository.GetActions(ctx, team.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newTeam.SpendTime = int64(20 * len(actions))
|
||||
applications, err := s.repository.GetApplications(ctx, team.ID, "NEW")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newTeam.Applications = mapApplicationsToProtoApplications(applications)
|
||||
res = append(res, newTeam)
|
||||
}
|
||||
return &proto.GetTeamsRsp{Teams: res}, err
|
||||
}
|
||||
@@ -137,10 +154,7 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
|
||||
if !ok {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "error creds")
|
||||
}
|
||||
teamId, err := strconv.Atoi(teamIdArr[0])
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "error creds")
|
||||
}
|
||||
teamId := teamIdArr[0]
|
||||
|
||||
passwordArr, ok := md["password"]
|
||||
if !ok {
|
||||
@@ -154,3 +168,10 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
|
||||
}
|
||||
return team, nil
|
||||
}
|
||||
|
||||
func log(team *models.Team, action string, v any) {
|
||||
vJson, err := json.Marshal(v)
|
||||
if err == nil {
|
||||
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,29 @@ package story_service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
replaceMap = map[string]string{
|
||||
"a": "а",
|
||||
"e": "е",
|
||||
"o": "о",
|
||||
"c": "с",
|
||||
"p": "р",
|
||||
"x": "х",
|
||||
"y": "у",
|
||||
"k": "к",
|
||||
"m": "м",
|
||||
"t": "т",
|
||||
"h": "н",
|
||||
"b": "в",
|
||||
"u": "и",
|
||||
}
|
||||
)
|
||||
|
||||
type Story struct {
|
||||
Places []*Place `json:"places"`
|
||||
}
|
||||
@@ -46,10 +63,14 @@ func (s *StoryService) GetPlace(code string) (*Place, error) {
|
||||
return place, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf("place not found: %s", code))
|
||||
return nil, fmt.Errorf("place not found: %s", code)
|
||||
}
|
||||
|
||||
func clearCode(code string) string {
|
||||
code = strings.ToLower(code)
|
||||
return strings.ReplaceAll(code, "-", "")
|
||||
code = strings.ReplaceAll(code, "-", "")
|
||||
for latin, cyrillic := range replaceMap {
|
||||
code = strings.ReplaceAll(code, latin, cyrillic)
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user