diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index c8c518f2..5def38e4 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -237,6 +237,7 @@ var ( // TransactionDetailsColumns holds the columns for the "transaction_details" table. TransactionDetailsColumns = []*schema.Column{ {Name: "id", Type: field.TypeUUID}, + {Name: "title", Type: field.TypeString, Size: 64}, {Name: "amount", Type: field.TypeInt, Default: 0}, {Name: "target", Type: field.TypeString, Default: ""}, {Name: "created_at", Type: field.TypeTime}, @@ -251,7 +252,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "transaction_details_transactions_detail", - Columns: []*schema.Column{TransactionDetailsColumns[5]}, + Columns: []*schema.Column{TransactionDetailsColumns[6]}, RefColumns: []*schema.Column{TransactionsColumns[0]}, OnDelete: schema.Cascade, }, diff --git a/ent/mutation.go b/ent/mutation.go index e19b834e..b330e835 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -6560,6 +6560,7 @@ type TransactionDetailMutation struct { op Op typ string id *uuid.UUID + title *string amount *int addamount *int target *string @@ -6677,6 +6678,42 @@ func (m *TransactionDetailMutation) IDs(ctx context.Context) ([]uuid.UUID, error } } +// SetTitle sets the "title" field. +func (m *TransactionDetailMutation) SetTitle(s string) { + m.title = &s +} + +// Title returns the value of the "title" field in the mutation. +func (m *TransactionDetailMutation) Title() (r string, exists bool) { + v := m.title + if v == nil { + return + } + return *v, true +} + +// OldTitle returns the old "title" field's value of the TransactionDetail entity. +// If the TransactionDetail object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *TransactionDetailMutation) OldTitle(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldTitle is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldTitle requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldTitle: %w", err) + } + return oldValue.Title, nil +} + +// ResetTitle resets all changes to the "title" field. +func (m *TransactionDetailMutation) ResetTitle() { + m.title = nil +} + // SetAmount sets the "amount" field. func (m *TransactionDetailMutation) SetAmount(i int) { m.amount = &i @@ -6914,7 +6951,10 @@ func (m *TransactionDetailMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *TransactionDetailMutation) Fields() []string { - fields := make([]string, 0, 4) + fields := make([]string, 0, 5) + if m.title != nil { + fields = append(fields, transactiondetail.FieldTitle) + } if m.amount != nil { fields = append(fields, transactiondetail.FieldAmount) } @@ -6935,6 +6975,8 @@ func (m *TransactionDetailMutation) Fields() []string { // schema. func (m *TransactionDetailMutation) Field(name string) (ent.Value, bool) { switch name { + case transactiondetail.FieldTitle: + return m.Title() case transactiondetail.FieldAmount: return m.Amount() case transactiondetail.FieldTarget: @@ -6952,6 +6994,8 @@ func (m *TransactionDetailMutation) Field(name string) (ent.Value, bool) { // database failed. func (m *TransactionDetailMutation) OldField(ctx context.Context, name string) (ent.Value, error) { switch name { + case transactiondetail.FieldTitle: + return m.OldTitle(ctx) case transactiondetail.FieldAmount: return m.OldAmount(ctx) case transactiondetail.FieldTarget: @@ -6969,6 +7013,13 @@ func (m *TransactionDetailMutation) OldField(ctx context.Context, name string) ( // type. func (m *TransactionDetailMutation) SetField(name string, value ent.Value) error { switch name { + case transactiondetail.FieldTitle: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetTitle(v) + return nil case transactiondetail.FieldAmount: v, ok := value.(int) if !ok { @@ -7061,6 +7112,9 @@ func (m *TransactionDetailMutation) ClearField(name string) error { // It returns an error if the field is not defined in the schema. func (m *TransactionDetailMutation) ResetField(name string) error { switch name { + case transactiondetail.FieldTitle: + m.ResetTitle() + return nil case transactiondetail.FieldAmount: m.ResetAmount() return nil diff --git a/ent/runtime.go b/ent/runtime.go index 2a1993c7..7d8204e9 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -152,20 +152,39 @@ func init() { transaction.DefaultID = transactionDescID.Default.(func() uuid.UUID) transactiondetailFields := schema.TransactionDetail{}.Fields() _ = transactiondetailFields + // transactiondetailDescTitle is the schema descriptor for title field. + transactiondetailDescTitle := transactiondetailFields[1].Descriptor() + // transactiondetail.TitleValidator is a validator for the "title" field. It is called by the builders before save. + transactiondetail.TitleValidator = func() func(string) error { + validators := transactiondetailDescTitle.Validators + fns := [...]func(string) error{ + validators[0].(func(string) error), + validators[1].(func(string) error), + validators[2].(func(string) error), + } + return func(title string) error { + for _, fn := range fns { + if err := fn(title); err != nil { + return err + } + } + return nil + } + }() // transactiondetailDescAmount is the schema descriptor for amount field. - transactiondetailDescAmount := transactiondetailFields[1].Descriptor() + transactiondetailDescAmount := transactiondetailFields[2].Descriptor() // transactiondetail.DefaultAmount holds the default value on creation for the amount field. transactiondetail.DefaultAmount = transactiondetailDescAmount.Default.(int) // transactiondetailDescTarget is the schema descriptor for target field. - transactiondetailDescTarget := transactiondetailFields[2].Descriptor() + transactiondetailDescTarget := transactiondetailFields[3].Descriptor() // transactiondetail.DefaultTarget holds the default value on creation for the target field. transactiondetail.DefaultTarget = transactiondetailDescTarget.Default.(string) // transactiondetailDescCreatedAt is the schema descriptor for created_at field. - transactiondetailDescCreatedAt := transactiondetailFields[3].Descriptor() + transactiondetailDescCreatedAt := transactiondetailFields[4].Descriptor() // transactiondetail.DefaultCreatedAt holds the default value on creation for the created_at field. transactiondetail.DefaultCreatedAt = transactiondetailDescCreatedAt.Default.(func() time.Time) // transactiondetailDescUpdatedAt is the schema descriptor for updated_at field. - transactiondetailDescUpdatedAt := transactiondetailFields[4].Descriptor() + transactiondetailDescUpdatedAt := transactiondetailFields[5].Descriptor() // transactiondetail.DefaultUpdatedAt holds the default value on creation for the updated_at field. transactiondetail.DefaultUpdatedAt = transactiondetailDescUpdatedAt.Default.(func() time.Time) // transactiondetail.UpdateDefaultUpdatedAt holds the default value on update for the updated_at field. diff --git a/ent/schema/transactiondetail.go b/ent/schema/transactiondetail.go index 04ac7667..118605f9 100644 --- a/ent/schema/transactiondetail.go +++ b/ent/schema/transactiondetail.go @@ -1,6 +1,8 @@ package schema import ( + "errors" + "strings" "time" "entgo.io/ent" @@ -19,6 +21,12 @@ func (TransactionDetail) Fields() []ent.Field { return []ent.Field{ field.UUID("id", uuid.UUID{}). Default(uuid.New), + field.String("title").NotEmpty().MaxLen(64).Validate(func(s string) error { + if strings.Contains(s, "\n") { + return errors.New("title cannot contain new line") + } + return nil + }), field.Int("amount"). Default(0), field.String("target"). diff --git a/ent/transactiondetail.go b/ent/transactiondetail.go index fe3095f2..9a210de3 100644 --- a/ent/transactiondetail.go +++ b/ent/transactiondetail.go @@ -19,6 +19,8 @@ type TransactionDetail struct { config `json:"-"` // ID of the ent. ID uuid.UUID `json:"id,omitempty"` + // Title holds the value of the "title" field. + Title string `json:"title,omitempty"` // Amount holds the value of the "amount" field. Amount int `json:"amount,omitempty"` // Target holds the value of the "target" field. @@ -61,7 +63,7 @@ func (*TransactionDetail) scanValues(columns []string) ([]any, error) { switch columns[i] { case transactiondetail.FieldAmount: values[i] = new(sql.NullInt64) - case transactiondetail.FieldTarget: + case transactiondetail.FieldTitle, transactiondetail.FieldTarget: values[i] = new(sql.NullString) case transactiondetail.FieldCreatedAt, transactiondetail.FieldUpdatedAt: values[i] = new(sql.NullTime) @@ -90,6 +92,12 @@ func (td *TransactionDetail) assignValues(columns []string, values []any) error } else if value != nil { td.ID = *value } + case transactiondetail.FieldTitle: + if value, ok := values[i].(*sql.NullString); !ok { + return fmt.Errorf("unexpected type %T for field title", values[i]) + } else if value.Valid { + td.Title = value.String + } case transactiondetail.FieldAmount: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field amount", values[i]) @@ -162,6 +170,9 @@ func (td *TransactionDetail) String() string { var builder strings.Builder builder.WriteString("TransactionDetail(") builder.WriteString(fmt.Sprintf("id=%v, ", td.ID)) + builder.WriteString("title=") + builder.WriteString(td.Title) + builder.WriteString(", ") builder.WriteString("amount=") builder.WriteString(fmt.Sprintf("%v", td.Amount)) builder.WriteString(", ") diff --git a/ent/transactiondetail/transactiondetail.go b/ent/transactiondetail/transactiondetail.go index 7929c68c..81db2aa5 100644 --- a/ent/transactiondetail/transactiondetail.go +++ b/ent/transactiondetail/transactiondetail.go @@ -15,6 +15,8 @@ const ( Label = "transaction_detail" // FieldID holds the string denoting the id field in the database. FieldID = "id" + // FieldTitle holds the string denoting the title field in the database. + FieldTitle = "title" // FieldAmount holds the string denoting the amount field in the database. FieldAmount = "amount" // FieldTarget holds the string denoting the target field in the database. @@ -39,6 +41,7 @@ const ( // Columns holds all SQL columns for transactiondetail fields. var Columns = []string{ FieldID, + FieldTitle, FieldAmount, FieldTarget, FieldCreatedAt, @@ -67,6 +70,8 @@ func ValidColumn(column string) bool { } var ( + // TitleValidator is a validator for the "title" field. It is called by the builders before save. + TitleValidator func(string) error // DefaultAmount holds the default value on creation for the "amount" field. DefaultAmount int // DefaultTarget holds the default value on creation for the "target" field. @@ -89,6 +94,11 @@ func ByID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldID, opts...).ToFunc() } +// ByTitle orders the results by the title field. +func ByTitle(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldTitle, opts...).ToFunc() +} + // ByAmount orders the results by the amount field. func ByAmount(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldAmount, opts...).ToFunc() diff --git a/ent/transactiondetail/where.go b/ent/transactiondetail/where.go index 60d337fe..7f377c46 100644 --- a/ent/transactiondetail/where.go +++ b/ent/transactiondetail/where.go @@ -56,6 +56,11 @@ func IDLTE(id uuid.UUID) predicate.TransactionDetail { return predicate.TransactionDetail(sql.FieldLTE(FieldID, id)) } +// Title applies equality check predicate on the "title" field. It's identical to TitleEQ. +func Title(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldEQ(FieldTitle, v)) +} + // Amount applies equality check predicate on the "amount" field. It's identical to AmountEQ. func Amount(v int) predicate.TransactionDetail { return predicate.TransactionDetail(sql.FieldEQ(FieldAmount, v)) @@ -76,6 +81,71 @@ func UpdatedAt(v time.Time) predicate.TransactionDetail { return predicate.TransactionDetail(sql.FieldEQ(FieldUpdatedAt, v)) } +// TitleEQ applies the EQ predicate on the "title" field. +func TitleEQ(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldEQ(FieldTitle, v)) +} + +// TitleNEQ applies the NEQ predicate on the "title" field. +func TitleNEQ(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldNEQ(FieldTitle, v)) +} + +// TitleIn applies the In predicate on the "title" field. +func TitleIn(vs ...string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldIn(FieldTitle, vs...)) +} + +// TitleNotIn applies the NotIn predicate on the "title" field. +func TitleNotIn(vs ...string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldNotIn(FieldTitle, vs...)) +} + +// TitleGT applies the GT predicate on the "title" field. +func TitleGT(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldGT(FieldTitle, v)) +} + +// TitleGTE applies the GTE predicate on the "title" field. +func TitleGTE(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldGTE(FieldTitle, v)) +} + +// TitleLT applies the LT predicate on the "title" field. +func TitleLT(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldLT(FieldTitle, v)) +} + +// TitleLTE applies the LTE predicate on the "title" field. +func TitleLTE(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldLTE(FieldTitle, v)) +} + +// TitleContains applies the Contains predicate on the "title" field. +func TitleContains(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldContains(FieldTitle, v)) +} + +// TitleHasPrefix applies the HasPrefix predicate on the "title" field. +func TitleHasPrefix(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldHasPrefix(FieldTitle, v)) +} + +// TitleHasSuffix applies the HasSuffix predicate on the "title" field. +func TitleHasSuffix(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldHasSuffix(FieldTitle, v)) +} + +// TitleEqualFold applies the EqualFold predicate on the "title" field. +func TitleEqualFold(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldEqualFold(FieldTitle, v)) +} + +// TitleContainsFold applies the ContainsFold predicate on the "title" field. +func TitleContainsFold(v string) predicate.TransactionDetail { + return predicate.TransactionDetail(sql.FieldContainsFold(FieldTitle, v)) +} + // AmountEQ applies the EQ predicate on the "amount" field. func AmountEQ(v int) predicate.TransactionDetail { return predicate.TransactionDetail(sql.FieldEQ(FieldAmount, v)) diff --git a/ent/transactiondetail_create.go b/ent/transactiondetail_create.go index 25cd01b6..b6aa5326 100644 --- a/ent/transactiondetail_create.go +++ b/ent/transactiondetail_create.go @@ -22,6 +22,12 @@ type TransactionDetailCreate struct { hooks []Hook } +// SetTitle sets the "title" field. +func (tdc *TransactionDetailCreate) SetTitle(s string) *TransactionDetailCreate { + tdc.mutation.SetTitle(s) + return tdc +} + // SetAmount sets the "amount" field. func (tdc *TransactionDetailCreate) SetAmount(i int) *TransactionDetailCreate { tdc.mutation.SetAmount(i) @@ -170,6 +176,14 @@ func (tdc *TransactionDetailCreate) defaults() { // check runs all checks and user-defined validators on the builder. func (tdc *TransactionDetailCreate) check() error { + if _, ok := tdc.mutation.Title(); !ok { + return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "TransactionDetail.title"`)} + } + if v, ok := tdc.mutation.Title(); ok { + if err := transactiondetail.TitleValidator(v); err != nil { + return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "TransactionDetail.title": %w`, err)} + } + } if _, ok := tdc.mutation.Amount(); !ok { return &ValidationError{Name: "amount", err: errors.New(`ent: missing required field "TransactionDetail.amount"`)} } @@ -217,6 +231,10 @@ func (tdc *TransactionDetailCreate) createSpec() (*TransactionDetail, *sqlgraph. _node.ID = id _spec.ID.Value = &id } + if value, ok := tdc.mutation.Title(); ok { + _spec.SetField(transactiondetail.FieldTitle, field.TypeString, value) + _node.Title = value + } if value, ok := tdc.mutation.Amount(); ok { _spec.SetField(transactiondetail.FieldAmount, field.TypeInt, value) _node.Amount = value diff --git a/ent/transactiondetail_query.go b/ent/transactiondetail_query.go index e2dfe2a9..b5d9539b 100644 --- a/ent/transactiondetail_query.go +++ b/ent/transactiondetail_query.go @@ -300,12 +300,12 @@ func (tdq *TransactionDetailQuery) WithTransaction(opts ...func(*TransactionQuer // Example: // // var v []struct { -// Amount int `json:"amount,omitempty"` +// Title string `json:"title,omitempty"` // Count int `json:"count,omitempty"` // } // // client.TransactionDetail.Query(). -// GroupBy(transactiondetail.FieldAmount). +// GroupBy(transactiondetail.FieldTitle). // Aggregate(ent.Count()). // Scan(ctx, &v) func (tdq *TransactionDetailQuery) GroupBy(field string, fields ...string) *TransactionDetailGroupBy { @@ -323,11 +323,11 @@ func (tdq *TransactionDetailQuery) GroupBy(field string, fields ...string) *Tran // Example: // // var v []struct { -// Amount int `json:"amount,omitempty"` +// Title string `json:"title,omitempty"` // } // // client.TransactionDetail.Query(). -// Select(transactiondetail.FieldAmount). +// Select(transactiondetail.FieldTitle). // Scan(ctx, &v) func (tdq *TransactionDetailQuery) Select(fields ...string) *TransactionDetailSelect { tdq.ctx.Fields = append(tdq.ctx.Fields, fields...) diff --git a/ent/transactiondetail_update.go b/ent/transactiondetail_update.go index 418fd1b9..81e609c8 100644 --- a/ent/transactiondetail_update.go +++ b/ent/transactiondetail_update.go @@ -30,6 +30,20 @@ func (tdu *TransactionDetailUpdate) Where(ps ...predicate.TransactionDetail) *Tr return tdu } +// SetTitle sets the "title" field. +func (tdu *TransactionDetailUpdate) SetTitle(s string) *TransactionDetailUpdate { + tdu.mutation.SetTitle(s) + return tdu +} + +// SetNillableTitle sets the "title" field if the given value is not nil. +func (tdu *TransactionDetailUpdate) SetNillableTitle(s *string) *TransactionDetailUpdate { + if s != nil { + tdu.SetTitle(*s) + } + return tdu +} + // SetAmount sets the "amount" field. func (tdu *TransactionDetailUpdate) SetAmount(i int) *TransactionDetailUpdate { tdu.mutation.ResetAmount() @@ -151,7 +165,20 @@ func (tdu *TransactionDetailUpdate) defaults() { } } +// check runs all checks and user-defined validators on the builder. +func (tdu *TransactionDetailUpdate) check() error { + if v, ok := tdu.mutation.Title(); ok { + if err := transactiondetail.TitleValidator(v); err != nil { + return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "TransactionDetail.title": %w`, err)} + } + } + return nil +} + func (tdu *TransactionDetailUpdate) sqlSave(ctx context.Context) (n int, err error) { + if err := tdu.check(); err != nil { + return n, err + } _spec := sqlgraph.NewUpdateSpec(transactiondetail.Table, transactiondetail.Columns, sqlgraph.NewFieldSpec(transactiondetail.FieldID, field.TypeUUID)) if ps := tdu.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { @@ -160,6 +187,9 @@ func (tdu *TransactionDetailUpdate) sqlSave(ctx context.Context) (n int, err err } } } + if value, ok := tdu.mutation.Title(); ok { + _spec.SetField(transactiondetail.FieldTitle, field.TypeString, value) + } if value, ok := tdu.mutation.Amount(); ok { _spec.SetField(transactiondetail.FieldAmount, field.TypeInt, value) } @@ -224,6 +254,20 @@ type TransactionDetailUpdateOne struct { mutation *TransactionDetailMutation } +// SetTitle sets the "title" field. +func (tduo *TransactionDetailUpdateOne) SetTitle(s string) *TransactionDetailUpdateOne { + tduo.mutation.SetTitle(s) + return tduo +} + +// SetNillableTitle sets the "title" field if the given value is not nil. +func (tduo *TransactionDetailUpdateOne) SetNillableTitle(s *string) *TransactionDetailUpdateOne { + if s != nil { + tduo.SetTitle(*s) + } + return tduo +} + // SetAmount sets the "amount" field. func (tduo *TransactionDetailUpdateOne) SetAmount(i int) *TransactionDetailUpdateOne { tduo.mutation.ResetAmount() @@ -358,7 +402,20 @@ func (tduo *TransactionDetailUpdateOne) defaults() { } } +// check runs all checks and user-defined validators on the builder. +func (tduo *TransactionDetailUpdateOne) check() error { + if v, ok := tduo.mutation.Title(); ok { + if err := transactiondetail.TitleValidator(v); err != nil { + return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "TransactionDetail.title": %w`, err)} + } + } + return nil +} + func (tduo *TransactionDetailUpdateOne) sqlSave(ctx context.Context) (_node *TransactionDetail, err error) { + if err := tduo.check(); err != nil { + return _node, err + } _spec := sqlgraph.NewUpdateSpec(transactiondetail.Table, transactiondetail.Columns, sqlgraph.NewFieldSpec(transactiondetail.FieldID, field.TypeUUID)) id, ok := tduo.mutation.ID() if !ok { @@ -384,6 +441,9 @@ func (tduo *TransactionDetailUpdateOne) sqlSave(ctx context.Context) (_node *Tra } } } + if value, ok := tduo.mutation.Title(); ok { + _spec.SetField(transactiondetail.FieldTitle, field.TypeString, value) + } if value, ok := tduo.mutation.Amount(); ok { _spec.SetField(transactiondetail.FieldAmount, field.TypeInt, value) } diff --git a/model/mock_model/mock_transaction.go b/model/mock_model/mock_transaction.go index 8ed24d71..abeb5f96 100644 --- a/model/mock_model/mock_transaction.go +++ b/model/mock_model/mock_transaction.go @@ -42,18 +42,18 @@ func (m *MockTransactionRepository) EXPECT() *MockTransactionRepositoryMockRecor } // CreateTransaction mocks base method. -func (m *MockTransactionRepository) CreateTransaction(ctx context.Context, Amount int, Target string, tags []*uuid.UUID, group, requestID *uuid.UUID) (*model.TransactionResponse, error) { +func (m *MockTransactionRepository) CreateTransaction(ctx context.Context, title string, Amount int, Target string, tags []*uuid.UUID, group, requestID *uuid.UUID) (*model.TransactionResponse, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateTransaction", ctx, Amount, Target, tags, group, requestID) + ret := m.ctrl.Call(m, "CreateTransaction", ctx, title, Amount, Target, tags, group, requestID) ret0, _ := ret[0].(*model.TransactionResponse) ret1, _ := ret[1].(error) return ret0, ret1 } // CreateTransaction indicates an expected call of CreateTransaction. -func (mr *MockTransactionRepositoryMockRecorder) CreateTransaction(ctx, Amount, Target, tags, group, requestID any) *gomock.Call { +func (mr *MockTransactionRepositoryMockRecorder) CreateTransaction(ctx, title, Amount, Target, tags, group, requestID any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateTransaction", reflect.TypeOf((*MockTransactionRepository)(nil).CreateTransaction), ctx, Amount, Target, tags, group, requestID) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateTransaction", reflect.TypeOf((*MockTransactionRepository)(nil).CreateTransaction), ctx, title, Amount, Target, tags, group, requestID) } // GetTransaction mocks base method. @@ -87,16 +87,16 @@ func (mr *MockTransactionRepositoryMockRecorder) GetTransactions(ctx, query any) } // UpdateTransaction mocks base method. -func (m *MockTransactionRepository) UpdateTransaction(ctx context.Context, transactionID uuid.UUID, Amount int, Target string, tags []*uuid.UUID, group, requestID *uuid.UUID) (*model.TransactionResponse, error) { +func (m *MockTransactionRepository) UpdateTransaction(ctx context.Context, transactionID uuid.UUID, title string, Amount int, Target string, tags []*uuid.UUID, group, requestID *uuid.UUID) (*model.TransactionResponse, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpdateTransaction", ctx, transactionID, Amount, Target, tags, group, requestID) + ret := m.ctrl.Call(m, "UpdateTransaction", ctx, transactionID, title, Amount, Target, tags, group, requestID) ret0, _ := ret[0].(*model.TransactionResponse) ret1, _ := ret[1].(error) return ret0, ret1 } // UpdateTransaction indicates an expected call of UpdateTransaction. -func (mr *MockTransactionRepositoryMockRecorder) UpdateTransaction(ctx, transactionID, Amount, Target, tags, group, requestID any) *gomock.Call { +func (mr *MockTransactionRepositoryMockRecorder) UpdateTransaction(ctx, transactionID, title, Amount, Target, tags, group, requestID any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTransaction", reflect.TypeOf((*MockTransactionRepository)(nil).UpdateTransaction), ctx, transactionID, Amount, Target, tags, group, requestID) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTransaction", reflect.TypeOf((*MockTransactionRepository)(nil).UpdateTransaction), ctx, transactionID, title, Amount, Target, tags, group, requestID) } diff --git a/model/transaction.go b/model/transaction.go index 43c9c3ba..a36feb5a 100644 --- a/model/transaction.go +++ b/model/transaction.go @@ -11,12 +11,12 @@ import ( type TransactionRepository interface { GetTransactions(ctx context.Context, query TransactionQuery) ([]*TransactionResponse, error) CreateTransaction( - ctx context.Context, Amount int, Target string, + ctx context.Context, title string, Amount int, Target string, tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID, ) (*TransactionResponse, error) GetTransaction(ctx context.Context, transactionID uuid.UUID) (*TransactionResponse, error) UpdateTransaction( - ctx context.Context, transactionID uuid.UUID, Amount int, Target string, + ctx context.Context, transactionID uuid.UUID, title string, Amount int, Target string, tags []*uuid.UUID, group *uuid.UUID, requestID *uuid.UUID, ) (*TransactionResponse, error) } @@ -28,6 +28,7 @@ type Transaction struct { type TransactionResponse struct { ID uuid.UUID + Title string Amount int Target string Request *uuid.UUID diff --git a/model/transaction_detail.go b/model/transaction_detail.go index dbd456ba..dfee3bfd 100644 --- a/model/transaction_detail.go +++ b/model/transaction_detail.go @@ -12,6 +12,7 @@ type TransactionDetailRepository interface { type TransactionDetail struct { ID uuid.UUID + Title string Amount int Target string CreatedAt time.Time diff --git a/model/transaction_detail_impl.go b/model/transaction_detail_impl.go index 2b611089..168f0751 100644 --- a/model/transaction_detail_impl.go +++ b/model/transaction_detail_impl.go @@ -10,9 +10,10 @@ import ( ) func (repo *EntRepository) createTransactionDetail( - ctx context.Context, tx *ent.Tx, amount int, target string, + ctx context.Context, tx *ent.Tx, title string, amount int, target string, ) (*TransactionDetail, error) { enttd, err := tx.Client().TransactionDetail.Create(). + SetTitle(title). SetAmount(amount). SetTarget(target). Save(ctx) @@ -23,7 +24,8 @@ func (repo *EntRepository) createTransactionDetail( } func (repo *EntRepository) updateTransactionDetail( - ctx context.Context, tx *ent.Tx, transactionID uuid.UUID, amount int, target string, + ctx context.Context, tx *ent.Tx, transactionID uuid.UUID, + title string, amount int, target string, ) (*TransactionDetail, error) { _, err := tx.Client().TransactionDetail.Update(). Where(transactiondetail.HasTransactionWith( @@ -35,6 +37,7 @@ func (repo *EntRepository) updateTransactionDetail( return nil, err } enttd, err := tx.Client().TransactionDetail.Create(). + SetTitle(title). SetAmount(amount). SetTarget(target). SetTransactionID(transactionID). @@ -53,6 +56,7 @@ func convertEntTransactionDetailToModelTransactionDetail( } return &TransactionDetail{ ID: enttd.ID, + Title: enttd.Title, Amount: enttd.Amount, Target: enttd.Target, CreatedAt: enttd.CreatedAt, diff --git a/model/transaction_detail_impl_test.go b/model/transaction_detail_impl_test.go index 427aa50c..046f6883 100644 --- a/model/transaction_detail_impl_test.go +++ b/model/transaction_detail_impl_test.go @@ -27,10 +27,11 @@ func TestEntRepository_createTransactionDetail(t *testing.T) { } }() + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 10) // Create TransactionDetail - td, err := repo.createTransactionDetail(ctx, tx, amount, target) + td, err := repo.createTransactionDetail(ctx, tx, title, amount, target) assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) @@ -58,17 +59,21 @@ func TestEntRepository_updateTransactionDetail(t *testing.T) { } }() + title := "Hoge" amount := 100 target := "hoge" // Create Transaction - trns, err := repo.CreateTransaction(ctx, amount, target, nil, nil, nil) + trns, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) require.NoError(t, err) // Update TransactionDetail + updateTitle := "Fuga" updatedAmount := 1000 updatedTarget := "fuga" - td, err := repo.updateTransactionDetail(ctx, tx, trns.ID, updatedAmount, updatedTarget) + td, err := repo.updateTransactionDetail( + ctx, tx, trns.ID, + updateTitle, updatedAmount, updatedTarget) assert.NoError(t, err) err = tx.Commit() assert.NoError(t, err) diff --git a/model/transaction_impl.go b/model/transaction_impl.go index a7b88b8e..af7b5745 100644 --- a/model/transaction_impl.go +++ b/model/transaction_impl.go @@ -147,7 +147,7 @@ func (repo *EntRepository) GetTransaction( } func (repo *EntRepository) CreateTransaction( - ctx context.Context, amount int, target string, + ctx context.Context, title string, amount int, target string, tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID, ) (*TransactionResponse, error) { tx, err := repo.client.Tx(ctx) @@ -167,7 +167,7 @@ func (repo *EntRepository) CreateTransaction( }) // Create Transaction Detail - detail, err := repo.createTransactionDetail(ctx, tx, amount, target) + detail, err := repo.createTransactionDetail(ctx, tx, title, amount, target) if err != nil { err = RollbackWithError(tx, err) return nil, err @@ -248,7 +248,7 @@ func (repo *EntRepository) CreateTransaction( } func (repo *EntRepository) UpdateTransaction( - ctx context.Context, transactionID uuid.UUID, amount int, target string, + ctx context.Context, transactionID uuid.UUID, title string, amount int, target string, tags []*uuid.UUID, groupID *uuid.UUID, requestID *uuid.UUID, ) (*TransactionResponse, error) { tx, err := repo.client.Tx(ctx) @@ -263,7 +263,7 @@ func (repo *EntRepository) UpdateTransaction( }() // Update transaction Detail - _, err = repo.updateTransactionDetail(ctx, tx, transactionID, amount, target) + _, err = repo.updateTransactionDetail(ctx, tx, transactionID, title, amount, target) if err != nil { err = RollbackWithError(tx, err) return nil, err @@ -392,6 +392,7 @@ func ConvertEntTransactionToModelTransactionResponse( } return &TransactionResponse{ ID: transaction.ID, + Title: transaction.Edges.Detail.Title, Amount: transaction.Edges.Detail.Amount, Target: transaction.Edges.Detail.Target, Request: r, diff --git a/model/transaction_impl_test.go b/model/transaction_impl_test.go index b97d9c2b..a69f9444 100644 --- a/model/transaction_impl_test.go +++ b/model/transaction_impl_test.go @@ -59,6 +59,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) request, err := repo.CreateRequest( @@ -69,10 +70,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx1, err := repo.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx1, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - tx2, err := repo.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx2, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -84,6 +85,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 2) { assert.Equal(t, tx1.ID, got[1].ID) + assert.Equal(t, tx1.Title, got[1].Title) assert.Equal(t, tx1.Amount, got[1].Amount) assert.Equal(t, tx1.Target, got[1].Target) assert.Equal(t, tx1.CreatedAt, got[1].CreatedAt) @@ -108,6 +110,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) request, err := repo2.CreateRequest( @@ -118,10 +121,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx1, err := repo2.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx1, err := repo2.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - tx2, err := repo2.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx2, err := repo2.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -133,6 +136,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 2) { assert.Equal(t, tx1.ID, got[0].ID) + assert.Equal(t, tx1.Title, got[0].Title) assert.Equal(t, tx1.Amount, got[0].Amount) assert.Equal(t, tx1.Target, got[0].Target) assert.Equal(t, tx1.CreatedAt, got[0].CreatedAt) @@ -157,6 +161,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) target := random.AlphaNumeric(t, 20) request, err := repo3.CreateRequest( ctx, @@ -166,10 +171,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx1, err := repo3.CreateTransaction(ctx, 100, target, nil, nil, &request.ID) + tx1, err := repo3.CreateTransaction(ctx, title, 100, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - tx2, err := repo3.CreateTransaction(ctx, 10000, target, nil, nil, &request.ID) + tx2, err := repo3.CreateTransaction(ctx, title, 10000, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -181,6 +186,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 2) { assert.Equal(t, tx1.ID, got[0].ID) + assert.Equal(t, tx1.Title, got[0].Title) assert.Equal(t, tx1.Amount, got[0].Amount) assert.Equal(t, tx1.Target, got[0].Target) assert.Equal(t, tx1.CreatedAt, got[0].CreatedAt) @@ -208,6 +214,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) target := random.AlphaNumeric(t, 20) // nolint:contextcheck request, err := repo4.CreateRequest( @@ -219,11 +226,11 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // nolint:contextcheck - tx1, err := repo4.CreateTransaction(ctx, 100, target, nil, nil, &request.ID) + tx1, err := repo4.CreateTransaction(ctx, title, 100, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) // nolint:contextcheck - tx2, err := repo4.CreateTransaction(ctx, 10000, target, nil, nil, &request.ID) + tx2, err := repo4.CreateTransaction(ctx, title, 10000, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -236,6 +243,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 2) { assert.Equal(t, tx2.ID, got[0].ID) + assert.Equal(t, tx2.Title, got[0].Title) assert.Equal(t, tx2.Amount, got[0].Amount) assert.Equal(t, tx2.Target, got[0].Target) assert.Equal(t, tx2.CreatedAt, got[0].CreatedAt) @@ -260,6 +268,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) request, err := repo5.CreateRequest( @@ -270,10 +279,10 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx1, err := repo5.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx1, err := repo5.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) time.Sleep(1 * time.Second) - tx2, err := repo5.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx2, err := repo5.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -285,6 +294,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 2) { assert.Equal(t, tx1.ID, got[1].ID) + assert.Equal(t, tx1.Title, got[1].Title) assert.Equal(t, tx1.Amount, got[1].Amount) assert.Equal(t, tx1.Target, got[1].Target) assert.Equal(t, tx1.CreatedAt, got[1].CreatedAt) @@ -309,6 +319,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target1 := random.AlphaNumeric(t, 20) target2 := random.AlphaNumeric(t, 20) @@ -320,9 +331,9 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx, err := repo6.CreateTransaction(ctx, amount, target1, nil, nil, &request.ID) + tx, err := repo6.CreateTransaction(ctx, title, amount, target1, nil, nil, &request.ID) require.NoError(t, err) - _, err = repo6.CreateTransaction(ctx, amount, target2, nil, nil, &request.ID) + _, err = repo6.CreateTransaction(ctx, title, amount, target2, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -333,6 +344,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 1) { assert.Equal(t, tx.ID, got[0].ID) + assert.Equal(t, tx.Title, got[0].Title) assert.Equal(t, tx.Amount, got[0].Amount) assert.Equal(t, tx.Target, got[0].Target) assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) @@ -352,6 +364,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) request, err := repo7.CreateRequest( @@ -362,7 +375,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - _, err = repo7.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + _, err = repo7.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -374,7 +387,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { } time.Sleep(1 * time.Second) - tx, err := repo7.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx, err := repo7.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) got, err := repo7.GetTransactions(ctx, query) @@ -400,6 +413,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) tag, err := repo8.CreateTag(ctx, random.AlphaNumeric(t, 20)) @@ -412,7 +426,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - _, err = repo8.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + _, err = repo8.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -422,7 +436,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { tx, err := repo8.CreateTransaction( ctx, - amount, target, + title, amount, target, []*uuid.UUID{&tag.ID}, nil, &request.ID) require.NoError(t, err) @@ -430,6 +444,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { assert.NoError(t, err) if assert.Len(t, got, 1) { assert.Equal(t, tx.ID, got[0].ID) + assert.Equal(t, tx.Title, got[0].Title) assert.Equal(t, tx.Amount, got[0].Amount) assert.Equal(t, tx.Target, got[0].Target) assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) @@ -449,6 +464,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) budget := random.Numeric(t, 100000) @@ -466,7 +482,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - _, err = repo9.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + _, err = repo9.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) // Get Transactions @@ -474,13 +490,14 @@ func TestEntRepository_GetTransactions(t *testing.T) { Group: &group.Name, } - tx, err := repo9.CreateTransaction(ctx, amount, target, nil, &group.ID, &request.ID) + tx, err := repo9.CreateTransaction(ctx, title, amount, target, nil, &group.ID, &request.ID) require.NoError(t, err) got, err := repo9.GetTransactions(ctx, query) assert.NoError(t, err) if assert.Len(t, got, 1) { assert.Equal(t, tx.ID, got[0].ID) + assert.Equal(t, tx.Title, got[0].Title) assert.Equal(t, tx.Amount, got[0].Amount) assert.Equal(t, tx.Target, got[0].Target) assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) @@ -500,6 +517,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) request, err := repo10.CreateRequest( @@ -510,7 +528,7 @@ func TestEntRepository_GetTransactions(t *testing.T) { nil, user.ID) require.NoError(t, err) - _, err = repo10.CreateTransaction(ctx, amount, target, nil, nil, nil) + _, err = repo10.CreateTransaction(ctx, title, amount, target, nil, nil, nil) require.NoError(t, err) // Get Transactions @@ -518,13 +536,14 @@ func TestEntRepository_GetTransactions(t *testing.T) { Request: &request.ID, } - tx, err := repo10.CreateTransaction(ctx, amount, target, nil, nil, &request.ID) + tx, err := repo10.CreateTransaction(ctx, title, amount, target, nil, nil, &request.ID) require.NoError(t, err) got, err := repo10.GetTransactions(ctx, query) assert.NoError(t, err) if assert.Len(t, got, 1) { assert.Equal(t, tx.ID, got[0].ID) + assert.Equal(t, tx.Title, got[0].Title) assert.Equal(t, tx.Amount, got[0].Amount) assert.Equal(t, tx.Target, got[0].Target) assert.Equal(t, tx.CreatedAt, got[0].CreatedAt) @@ -561,6 +580,7 @@ func TestEntRepository_GetTransaction(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) target := random.AlphaNumeric(t, 20) _, err = repo.CreateRequest( @@ -571,7 +591,7 @@ func TestEntRepository_GetTransaction(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx, err := repo.CreateTransaction(ctx, amount, target, nil, nil, nil) + tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) require.NoError(t, err) // Get Transaction @@ -579,6 +599,7 @@ func TestEntRepository_GetTransaction(t *testing.T) { assert.NoError(t, err) if assert.NotNil(t, got) { assert.Equal(t, tx.ID, got.ID) + assert.Equal(t, tx.Title, got.Title) assert.Equal(t, tx.Amount, got.Amount) assert.Equal(t, tx.Target, got.Target) assert.Equal(t, tx.CreatedAt, got.CreatedAt) @@ -620,6 +641,7 @@ func TestEntRepository_CreateTransaction(t *testing.T) { require.NoError(t, err) // Create Transactions + title := random.AlphaNumeric(t, 20) target := random.AlphaNumeric(t, 20) request, err := repo.CreateRequest( ctx, @@ -631,10 +653,11 @@ func TestEntRepository_CreateTransaction(t *testing.T) { tx, err := repo.CreateTransaction( ctx, - amount, target, + title, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) if assert.Len(t, tx.Tags, 1) { @@ -654,6 +677,7 @@ func TestEntRepository_CreateTransaction(t *testing.T) { t.Parallel() ctx := context.Background() + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) // Create user @@ -682,9 +706,10 @@ func TestEntRepository_CreateTransaction(t *testing.T) { nil, user.ID) require.NoError(t, err) - tx, err := repo.CreateTransaction(ctx, amount, target, nil, &group.ID, &request.ID) + tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, &group.ID, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) assert.Len(t, tx.Tags, 0) @@ -701,6 +726,7 @@ func TestEntRepository_CreateTransaction(t *testing.T) { t.Parallel() ctx := context.Background() + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) // Create user @@ -727,10 +753,11 @@ func TestEntRepository_CreateTransaction(t *testing.T) { tx, err := repo.CreateTransaction( ctx, - amount, target, + title, amount, target, []*uuid.UUID{&tag.ID}, nil, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) if assert.Len(t, tx.Tags, 1) { @@ -746,12 +773,14 @@ func TestEntRepository_CreateTransaction(t *testing.T) { ctx := context.Background() // Create Transactions - target := random.AlphaNumeric(t, 20) + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) + target := random.AlphaNumeric(t, 20) - tx, err := repo.CreateTransaction(ctx, amount, target, nil, nil, nil) + tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) assert.Len(t, tx.Tags, 0) @@ -764,12 +793,14 @@ func TestEntRepository_CreateTransaction(t *testing.T) { ctx := context.Background() // Create Transactions - target := random.AlphaNumeric(t, 20) + title := random.AlphaNumeric(t, 20) amount := -1 * random.Numeric(t, 100000) + target := random.AlphaNumeric(t, 20) - tx, err := repo.CreateTransaction(ctx, amount, target, nil, nil, nil) + tx, err := repo.CreateTransaction(ctx, title, amount, target, nil, nil, nil) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) assert.Len(t, tx.Tags, 0) @@ -788,6 +819,7 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { t.Parallel() ctx := context.Background() + title := random.AlphaNumeric(t, 20) amount := random.Numeric(t, 100000) // Create user @@ -822,11 +854,12 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { tx, err := repo.CreateTransaction( ctx, - amount, target, + title, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) require.NoError(t, err) // Update Transactions + title = random.AlphaNumeric(t, 20) amount = random.Numeric(t, 100000) // Create tag @@ -853,10 +886,11 @@ func TestEntRepository_UpdateTransaction(t *testing.T) { tx, err = repo.UpdateTransaction( ctx, - tx.ID, amount, target, + tx.ID, title, amount, target, []*uuid.UUID{&tag.ID}, &group.ID, &request.ID) assert.NoError(t, err) if assert.NotNil(t, tx) { + assert.Equal(t, title, tx.Title) assert.Equal(t, amount, tx.Amount) assert.Equal(t, target, tx.Target) if assert.Len(t, tx.Tags, 1) { diff --git a/router/transaction.go b/router/transaction.go index 8e6e26b5..ec374eb4 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -16,6 +16,7 @@ import ( type Transaction struct { ID uuid.UUID `json:"id"` + Title string `json:"title"` Amount int `json:"amount"` Target string `json:"target"` Request *uuid.UUID `json:"request"` @@ -26,6 +27,7 @@ type Transaction struct { } type TransactionOverview struct { + Title string `json:"title"` Amount int `json:"amount"` Targets []*string `json:"targets"` Tags []*uuid.UUID `json:"tags"` @@ -34,6 +36,7 @@ type TransactionOverview struct { } type TransactionOverviewWithOneTarget struct { + Title string `json:"title"` Amount int `json:"amount"` Target string `json:"target"` Tags []*uuid.UUID `json:"tags"` @@ -160,6 +163,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Request: tx.Request, @@ -175,6 +179,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { func (h Handlers) PostTransaction(c echo.Context) error { var tx *TransactionOverview + // TODO: validate if err := c.Bind(&tx); err != nil { h.Logger.Info("could not get transaction overview from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) @@ -189,7 +194,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { } created, err := h.Repository.CreateTransaction( ctx, - tx.Amount, *target, tx.Tags, tx.Group, tx.Request) + tx.Title, tx.Amount, *target, tx.Tags, tx.Group, tx.Request) if err != nil { h.Logger.Error("failed to create transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) @@ -217,6 +222,7 @@ func (h Handlers) PostTransaction(c echo.Context) error { } res := Transaction{ ID: created.ID, + Title: created.Title, Amount: created.Amount, Target: created.Target, Request: created.Request, @@ -267,6 +273,7 @@ func (h Handlers) GetTransaction(c echo.Context) error { } res := Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Request: tx.Request, @@ -287,6 +294,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { } var tx *TransactionOverviewWithOneTarget + // TODO: validate if err := c.Bind(&tx); err != nil { h.Logger.Info( "could not get transaction overview with one target from request", @@ -297,7 +305,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { ctx := c.Request().Context() updated, err := h.Repository.UpdateTransaction( ctx, - txID, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request) + txID, tx.Title, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request) if err != nil { h.Logger.Error("failed to update transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) @@ -325,6 +333,7 @@ func (h Handlers) PutTransaction(c echo.Context) error { } res := Transaction{ ID: updated.ID, + Title: updated.Title, Amount: updated.Amount, Target: updated.Target, Request: updated.Request, diff --git a/router/transaction_test.go b/router/transaction_test.go index 5897ee09..b92775c0 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -39,6 +39,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -58,6 +59,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx2 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -120,6 +122,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -154,6 +157,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -173,6 +177,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx2 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -233,6 +238,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -267,6 +273,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -286,6 +293,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx2 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -347,6 +355,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -384,6 +393,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: target1, Tags: []*model.Tag{ @@ -403,6 +413,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx2 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -466,6 +477,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -503,6 +515,7 @@ func TestHandlers_GetTransactions(t *testing.T) { tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: target1, Tags: []*model.Tag{ @@ -569,6 +582,7 @@ func TestHandlers_GetTransactions(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -612,6 +626,7 @@ func TestHandlers_PostTransaction(t *testing.T) { tx1 := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: target1, Tags: []*model.Tag{ @@ -635,9 +650,10 @@ func TestHandlers_PostTransaction(t *testing.T) { group := tx1.Group.ID e := echo.New() + // FIXME: json.Marshalを使う reqBody := fmt.Sprintf( - `{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`, - tx1.Amount, tx1.Target, tag.ID, group) + `{"title": "%s", "amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`, + tx1.Title, tx1.Amount, tx1.Target, tag.ID, group) req, err := http.NewRequest( http.MethodPost, "/api/transactions", @@ -652,7 +668,9 @@ func TestHandlers_PostTransaction(t *testing.T) { h.Repository.MockTransactionRepository. EXPECT(). - CreateTransaction(c.Request().Context(), tx1.Amount, tx1.Target, tags, &group, nil). + CreateTransaction( + c.Request().Context(), + tx1.Title, tx1.Amount, tx1.Target, tags, &group, nil). Return(tx1, nil) res := lo.Map(txs, func(tx *model.TransactionResponse, _ int) *Transaction { @@ -675,6 +693,7 @@ func TestHandlers_PostTransaction(t *testing.T) { } return &Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: tag, @@ -712,6 +731,7 @@ func TestHandlers_PostTransaction(t *testing.T) { tx := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: target1, Tags: []*model.Tag{ @@ -748,9 +768,11 @@ func TestHandlers_PostTransaction(t *testing.T) { } e := echo.New() + // FIXME: json.Marshalを使う + // nolint:lll reqBody := fmt.Sprintf( - `{"amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s", "request": "%s"}`, - tx.Amount, tx.Target, tag.ID, group, request.ID) + `{"title": "%s", "amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s", "request": "%s"}`, + tx.Title, tx.Amount, tx.Target, tag.ID, group, request.ID) req, err := http.NewRequest( http.MethodPost, "/api/transactions", @@ -767,7 +789,7 @@ func TestHandlers_PostTransaction(t *testing.T) { EXPECT(). CreateTransaction( c.Request().Context(), - tx.Amount, tx.Target, + tx.Title, tx.Amount, tx.Target, tags, &group, &request.ID). Return(tx, nil) @@ -791,6 +813,7 @@ func TestHandlers_PostTransaction(t *testing.T) { res := []*Transaction{ { ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: to, @@ -807,6 +830,9 @@ func TestHandlers_PostTransaction(t *testing.T) { assert.Equal(t, string(resBody), strings.TrimRight(rec.Body.String(), "\n")) } }) + + // TODO: FailWithoutTitle + // PostTransactionにvalidationが入ってから } func TestHandlers_GetTransaction(t *testing.T) { @@ -827,6 +853,7 @@ func TestHandlers_GetTransaction(t *testing.T) { tx := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -861,7 +888,6 @@ func TestHandlers_GetTransaction(t *testing.T) { GetTransaction(c.Request().Context(), tx.ID). Return(tx, nil) - var resOverview Transaction to := lo.Map(tx.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -879,8 +905,9 @@ func TestHandlers_GetTransaction(t *testing.T) { CreatedAt: tx.Group.CreatedAt, UpdatedAt: tx.Group.UpdatedAt, } - resOverview = Transaction{ + resOverview := Transaction{ ID: tx.ID, + Title: tx.Title, Amount: tx.Amount, Target: tx.Target, Tags: to, @@ -924,6 +951,7 @@ func TestHandlers_PutTransaction(t *testing.T) { tx := &model.TransactionResponse{ ID: uuid.New(), + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -986,11 +1014,10 @@ func TestHandlers_PutTransaction(t *testing.T) { EXPECT(). UpdateTransaction( c.Request().Context(), - tx.ID, updated.Amount, updated.Target, + tx.ID, updated.Title, updated.Amount, updated.Target, updatedTags, nil, nil). Return(updated, nil) - var resOverview Transaction to := lo.Map(updated.Tags, func(modelTag *model.Tag, _ int) *TagOverview { return &TagOverview{ ID: modelTag.ID, @@ -1007,8 +1034,9 @@ func TestHandlers_PutTransaction(t *testing.T) { CreatedAt: updated.Group.CreatedAt, UpdatedAt: updated.Group.UpdatedAt, } - resOverview = Transaction{ + resOverview := Transaction{ ID: tx.ID, + Title: updated.Title, Amount: updated.Amount, Target: updated.Target, Tags: to,