generated from VLADIMIR/template
fix double applications
This commit is contained in:
@@ -24,11 +24,11 @@ func NewRepository(filepath string) (*Repository, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS actions (id INTEGER PRIMARY KEY AUTOINCREMENT, place TEXT, teamId INTEGER);")
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS actions (id INTEGER PRIMARY KEY AUTOINCREMENT, place TEXT, teamId INTEGER, FOREIGN KEY (teamId) REFERENCES teams(id) ON DELETE CASCADE);")
|
||||
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);")
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS applications (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, teamId INTEGER, state TEXT, FOREIGN KEY (teamId) REFERENCES teams(id) ON DELETE CASCADE);")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -127,19 +127,36 @@ func (r *Repository) GetTeam(ctx context.Context, teamId any, password any) (*mo
|
||||
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
|
||||
}
|
||||
func (r *Repository) AddApplications(ctx context.Context, teamId int64, applications []*models.Application) error {
|
||||
for _, application := range applications {
|
||||
_, err := r.db.Exec("insert into applications (name, teamId, state) values ($1, $2, $3)", application.Name, teamId, application.State)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) GetApplications(ctx context.Context, teamId int64, state string) ([]*models.Application, error) {
|
||||
func (r *Repository) GetApplications(ctx context.Context, teamId int64) ([]*models.Application, error) {
|
||||
rows, err := r.db.Query("select id, name from applications where teamId = $1", teamId)
|
||||
if err != nil {
|
||||
return nil, 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
|
||||
}
|
||||
|
||||
func (r *Repository) GetApplicationsByState(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 {
|
||||
return nil, err
|
||||
|
||||
@@ -78,7 +78,27 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro
|
||||
if err := s.repository.AddActions(ctx, team.ID, actions); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
if err := s.repository.AddApplications(ctx, actions); err != nil {
|
||||
currentApplications, err := s.repository.GetApplications(ctx, team.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newApplications := make([]*models.Application, 0, len(actions))
|
||||
for _, action := range actions {
|
||||
for _, actionApplication := range action.Applications {
|
||||
f := false
|
||||
for _, currentApplication := range currentApplications {
|
||||
if currentApplication.Name == actionApplication.Name {
|
||||
f = true
|
||||
}
|
||||
}
|
||||
if !f {
|
||||
newApplications = append(newApplications, actionApplication)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.repository.AddApplications(ctx, team.ID, newApplications); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
addLog(team, "add action", actions)
|
||||
@@ -134,11 +154,11 @@ func (s *Services) GetTeams(ctx context.Context, _ *proto.GetTeamsReq) (*proto.G
|
||||
return nil, err
|
||||
}
|
||||
newTeam.SpendTime = int64(len(actions))
|
||||
applications, err := s.repository.GetApplications(ctx, team.ID, "NEW")
|
||||
currentApplications, err := s.repository.GetApplicationsByState(ctx, team.ID, "NEW")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newTeam.Applications = mapApplicationsToProtoApplications(applications)
|
||||
newTeam.Applications = mapApplicationsToProtoApplications(currentApplications)
|
||||
res = append(res, newTeam)
|
||||
}
|
||||
return &proto.GetTeamsRsp{Teams: res}, err
|
||||
|
||||
Reference in New Issue
Block a user