Gisle Aune
5 years ago
16 changed files with 477 additions and 9 deletions
-
1database/database.go
-
7database/drivers/mysqldriver/db.go
-
141database/drivers/mysqldriver/issueitems.go
-
14database/repositories/issueitemrepository.go
-
2graph/gqlgen.yml
-
9graph/resolvers/issue.resolvers.go
-
44graph/resolvers/issueitem.resolvers.go
-
63graph/resolvers/mutation.resolvers.go
-
70graph/resolvers/query.resolvers.go
-
2graph/schema/issue.gql
-
75graph/schema/issueitem.gql
-
4graph/schema/mutation.gql
-
5graph/schema/query.gql
-
19migrations/mysql/20200517111706_create_table_issue_item.sql
-
14models/issueitem.go
@ -0,0 +1,141 @@ |
|||
package mysqldriver |
|||
|
|||
import ( |
|||
"context" |
|||
"database/sql" |
|||
"errors" |
|||
"fmt" |
|||
"git.aiterp.net/stufflog/server/internal/slerrors" |
|||
"git.aiterp.net/stufflog/server/models" |
|||
sq "github.com/Masterminds/squirrel" |
|||
"github.com/jmoiron/sqlx" |
|||
) |
|||
|
|||
type issueItemRepository struct { |
|||
db *sqlx.DB |
|||
} |
|||
|
|||
func (r *issueItemRepository) Find(ctx context.Context, id string) (*models.IssueItem, error) { |
|||
issueItem := models.IssueItem{} |
|||
err := r.db.GetContext(ctx, &issueItem, "SELECT * FROM issue_item WHERE issue_item_id=?", id) |
|||
if err != nil { |
|||
if err == sql.ErrNoRows { |
|||
return nil, slerrors.NotFound("Issue item") |
|||
} |
|||
|
|||
return nil, err |
|||
} |
|||
|
|||
return &issueItem, nil |
|||
} |
|||
|
|||
func (r *issueItemRepository) List(ctx context.Context, filter models.IssueItemFilter) ([]*models.IssueItem, error) { |
|||
q := sq.Select("issue_item.*").From("issue_item").GroupBy("issue_item.issue_item_id") |
|||
if len(filter.IssueItemIDs) > 0 { |
|||
q = q.Where(sq.Eq{"issue_item_id": filter.IssueIDs}) |
|||
} |
|||
if len(filter.IssueIDs) > 0 { |
|||
q = q.Where(sq.Eq{"issue_id": filter.IssueIDs}) |
|||
} |
|||
if len(filter.IssueAssignees) > 0 || len(filter.IssueOwners) > 0 || filter.IssueMinStage != nil || filter.IssueMaxStage != nil { |
|||
q = q.Join("issue ON issue.issue_id = issue_item.issue_id") |
|||
} |
|||
if len(filter.IssueAssignees) > 0 { |
|||
q = q.Where(sq.Eq{"issue.assignee_id": filter.IssueAssignees}) |
|||
} |
|||
if len(filter.IssueOwners) > 0 { |
|||
q = q.Where(sq.Eq{"issue.owner_id": filter.IssueOwners}) |
|||
} |
|||
if filter.IssueMinStage != nil && filter.IssueMaxStage != nil && *filter.IssueMinStage == *filter.IssueMaxStage { |
|||
q = q.Where(sq.Eq{"issue.status_stage": *filter.IssueMinStage}) |
|||
} else { |
|||
if filter.IssueMinStage != nil { |
|||
q = q.Where(sq.GtOrEq{"issue.status_stage": *filter.IssueMinStage}) |
|||
} |
|||
if filter.IssueMaxStage != nil { |
|||
q = q.Where(sq.LtOrEq{"issue.status_stage": *filter.IssueMaxStage}) |
|||
} |
|||
} |
|||
if len(filter.ItemIDs) > 0 { |
|||
q = q.Where(sq.Eq{"item_id": filter.IssueIDs}) |
|||
} |
|||
if len(filter.ItemTags) > 0 { |
|||
q = q.Join("item_tag ON item_tag.item_id = issue_item.item_id").Where( |
|||
sq.Eq{"item_tag.tag": filter.ItemTags}, |
|||
) |
|||
} |
|||
if filter.Acquired != nil { |
|||
q = q.Where(sq.Eq{"acquired": *filter.Acquired}) |
|||
} |
|||
|
|||
query, args, err := q.ToSql() |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
results := make([]*models.IssueItem, 0, 16) |
|||
err = r.db.SelectContext(ctx, &results, query, args...) |
|||
if err != nil { |
|||
if err == sql.ErrNoRows { |
|||
return []*models.IssueItem{}, nil |
|||
} |
|||
|
|||
return nil, err |
|||
} |
|||
|
|||
return results, nil |
|||
} |
|||
|
|||
func (r *issueItemRepository) Insert(ctx context.Context, item models.IssueItem) (*models.IssueItem, error) { |
|||
if item.IssueID == "" { |
|||
return nil, errors.New("missing issue id") |
|||
} |
|||
|
|||
tx, err := r.db.BeginTxx(ctx, nil) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
nextID, err := incCounter(ctx, tx, counterKindIssueSubID, item.IssueID) |
|||
if err != nil { |
|||
_ = tx.Rollback() |
|||
return nil, err |
|||
} |
|||
item.ID = fmt.Sprintf("%s-%d", item.IssueID, nextID) |
|||
|
|||
_, err = tx.NamedExecContext(ctx, ` |
|||
INSERT INTO issue_item ( |
|||
issue_item_id, issue_id, item_id, quantity, acquired |
|||
) VALUES ( |
|||
:issue_item_id, :issue_id, :item_id, :quantity, :acquired |
|||
); |
|||
`, item) |
|||
if err != nil { |
|||
_ = tx.Rollback() |
|||
return nil, err |
|||
} |
|||
|
|||
err = tx.Commit() |
|||
if err != nil { |
|||
_ = tx.Rollback() |
|||
return nil, err |
|||
} |
|||
|
|||
return &item, nil |
|||
} |
|||
|
|||
func (r *issueItemRepository) Save(ctx context.Context, item models.IssueItem) error { |
|||
_, err := r.db.NamedExecContext(ctx, ` |
|||
UPDATE issue_item SET |
|||
acquired=:acquired, |
|||
quantity=:quantity |
|||
WHERE issue_item_id=:issue_item_id |
|||
`, item) |
|||
|
|||
return err |
|||
} |
|||
|
|||
func (r *issueItemRepository) Delete(ctx context.Context, item models.IssueItem) error { |
|||
_, err := r.db.ExecContext(ctx, "DELETE FROM issue_item WHERE issue_item_id=? LIMIT 1;", item.ID) |
|||
return err |
|||
} |
@ -0,0 +1,14 @@ |
|||
package repositories |
|||
|
|||
import ( |
|||
"context" |
|||
"git.aiterp.net/stufflog/server/models" |
|||
) |
|||
|
|||
type IssueItemRepository interface { |
|||
Find(ctx context.Context, id string) (*models.IssueItem, error) |
|||
List(ctx context.Context, filter models.IssueItemFilter) ([]*models.IssueItem, error) |
|||
Insert(ctx context.Context, item models.IssueItem) (*models.IssueItem, error) |
|||
Save(ctx context.Context, item models.IssueItem) error |
|||
Delete(ctx context.Context, item models.IssueItem) error |
|||
} |
@ -0,0 +1,44 @@ |
|||
package resolvers |
|||
|
|||
// This file will be automatically regenerated based on the schema, any resolver implementations
|
|||
// will be copied through when generating and any unknown code will be moved to the end.
|
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
|
|||
"git.aiterp.net/stufflog/server/graph/graphcore" |
|||
"git.aiterp.net/stufflog/server/models" |
|||
) |
|||
|
|||
func (r *issueItemResolver) Issue(ctx context.Context, obj *models.IssueItem) (*models.Issue, error) { |
|||
return r.Database.Issues().Find(ctx, obj.IssueID) |
|||
} |
|||
|
|||
func (r *issueItemResolver) Item(ctx context.Context, obj *models.IssueItem) (*models.Item, error) { |
|||
return r.Database.Items().Find(ctx, obj.ItemID) |
|||
} |
|||
|
|||
func (r *issueItemResolver) Remaining(ctx context.Context, obj *models.IssueItem) (int, error) { |
|||
if obj.Acquired { |
|||
return 0, nil |
|||
} |
|||
|
|||
// TODO: Use logs
|
|||
return obj.Quantity, nil |
|||
} |
|||
|
|||
// IssueItem returns graphcore.IssueItemResolver implementation.
|
|||
func (r *Resolver) IssueItem() graphcore.IssueItemResolver { return &issueItemResolver{r} } |
|||
|
|||
type issueItemResolver struct{ *Resolver } |
|||
|
|||
// !!! WARNING !!!
|
|||
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
|
|||
// one last chance to move it out of harms way if you want. There are two reasons this happens:
|
|||
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
|
|||
// it when you're done.
|
|||
// - You have helper methods in this file. Move them out to keep these resolver files clean.
|
|||
func (r *issueItemResolver) Quanity(ctx context.Context, obj *models.IssueItem) (int, error) { |
|||
panic(fmt.Errorf("not implemented")) |
|||
} |
@ -0,0 +1,75 @@ |
|||
""" |
|||
An issue item is a requirement of an item under an issue. |
|||
""" |
|||
type IssueItem { |
|||
"ID of the issue item listing." |
|||
id: String! |
|||
"The amount of the item associated with an issue." |
|||
quantity: Int! |
|||
"Whether the full quantity of item has been acquired." |
|||
acquired: Boolean! |
|||
|
|||
"Parent issue of the issue item." |
|||
issue: Issue! |
|||
"The item associated with the issue." |
|||
item: Item! |
|||
"The amount of items remaining." |
|||
remaining: Int! |
|||
} |
|||
|
|||
"Input for the items query." |
|||
input IssueItemFilter { |
|||
"Filter to only these IDs, used primarily by IDs." |
|||
issueItemIds: [String!] |
|||
"Filter to only these issues." |
|||
issueIds: [String!] |
|||
"Filter to only issues where these are the asignees." |
|||
issueAssignees: [String!] |
|||
"Filter to only issues where these are the owners." |
|||
issueOwners: [String!] |
|||
"Filter by issue minimum stage (inclusive)." |
|||
issueMinStage: Int |
|||
"Filter by issue maximum stage (inclusive)." |
|||
issueMaxStage: Int |
|||
"Filter to only list issue items with these items." |
|||
itemIds: [String!] |
|||
"Filter to only list issue items where the item has these tags." |
|||
itemTags: [String!] |
|||
"Only listed acquired or non-acquired items." |
|||
acquired: Boolean |
|||
} |
|||
|
|||
"Input for the items query." |
|||
input IssueIssueItemFilter { |
|||
"Filter to only these IDs, used primarily by IDs." |
|||
issueItemIds: [String!] |
|||
"Filter to only list issue items with these items." |
|||
itemIds: [String!] |
|||
"Filter to only list issue items where the item has these tags." |
|||
itemTags: [String!] |
|||
"Only listed acquired or non-acquired items." |
|||
acquired: Boolean |
|||
} |
|||
|
|||
"Input for the createIssueItem mutation." |
|||
input IssueItemCreateInput { |
|||
"Parent issue." |
|||
issueId: String! |
|||
"Item to associate with." |
|||
itemId: String! |
|||
"Quantity of the item." |
|||
quanitty: Int! |
|||
"Whether the item has already been acquired." |
|||
acquired: Boolean |
|||
} |
|||
|
|||
"Input for the editIssueItem mutation." |
|||
input IssueItemEditInput { |
|||
"The ID of the issue item to edit." |
|||
issueItemId: String! |
|||
"Update the quantity of the item." |
|||
setQuanitty: Int |
|||
"Update whether the item has been acquired." |
|||
setAcquired: Boolean |
|||
} |
|||
|
@ -0,0 +1,19 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
CREATE TABLE issue_item ( |
|||
issue_item_id CHAR(48) NOT NULL PRIMARY KEY, |
|||
issue_id CHAR(32) NOT NULL, |
|||
item_id CHAR(32) NOT NULL, |
|||
quantity INTEGER NOT NULL, |
|||
acquired BOOLEAN NOT NULL, |
|||
|
|||
INDEX (acquired), |
|||
INDEX (issue_id), |
|||
INDEX (item_id) |
|||
); |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
DROP TABLE issue_item; |
|||
-- +goose StatementEnd |
Write
Preview
Loading…
Cancel
Save
Reference in new issue