Last active
September 7, 2024 18:42
-
-
Save rossnelson/219c711e6fd45baffba39239b8dab7c8 to your computer and use it in GitHub Desktop.
Service pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package contracts | |
type List []Record | |
type Record struct { | |
ID string `gorm:"column:id" json:"id"` | |
Name string `gorm:"column:name" json:"name"` | |
StartDate time.Time `gorm:"column:startDate" json:"start_date"` | |
EndDate time.Time `gorm:"column:endDate" json:"end_date"` | |
CreatedAt time.Time `gorm:"column:createdAt" json:"created_at"` | |
DeletedAt string `gorm:"column:deletedAt" json:"deleted_at"` | |
CustomerID string `gorm:"column:customerId" json:"customer_id"` | |
MerchantID string `gorm:"column:merchantId" json:"merchant_id"` | |
PurchaseID string `gorm:"column:purchaseId" json:"purchase_id"` | |
Status string `gorm:"column:status" json:"status"` | |
ProtectionPlanID string `gorm:"column:protectionPlanId" json:"protection_plan_id"` | |
ContractAmount float64 `gorm:"column:contractAmount" json:"contract_amount"` | |
NotifyPurchase bool `gorm:"column:notifyPurchase" json:"notify_purchase"` | |
PcmiContractNumber string `gorm:"column:pcmiContractNumber" json:"pcmi_contract_number"` | |
ToCancelPcmi bool `gorm:"column:toCancelPcmi" json:"to_cancel_pcmi"` | |
CancelDate string `gorm:"column:cancelDate" json:"cancel_date"` | |
ToRecreate bool `gorm:"column:toRecreate" json:"to_recreate"` | |
Source string `gorm:"column:source" json:"source"` | |
OldPcmiContractNumber string `gorm:"column:oldPcmiContractNumber" json:"old_pcmi_contract_number"` | |
CompletelyInvoiced string `gorm:"column:completelyInvoiced" json:"completely_invoiced"` | |
ActivatedAt string `gorm:"column:activatedAt" json:"activated_at"` | |
Sync bool `gorm:"column:sync" json:"sync"` | |
DisableEmailCommunications bool `gorm:"column:disableEmailCommunications" json:"disable_email_communications"` | |
ProcessingStatus string `gorm:"column:processing_status" json:"processing_status"` | |
RequestedToProcessAt time.Time `gorm:"column:requested_to_process_at" json:"requested_to_process_at"` | |
} | |
func (Record) TableName() string { | |
return "contracts" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package contracts | |
func Patch(id string, data Record) error { | |
result := db.Gx.Model(&Record{}). | |
Where(`id = ?`, id). | |
Updates(data) | |
if result.Error != nil { | |
return result.Error | |
} | |
return nil | |
} | |
func (c *Record) BeforeUpdate(tx *gorm.DB) (err error) { | |
handleProcessingStatus(tx, c) | |
return | |
} | |
const ( | |
ProcessingStatusPENDING = "PENDING" | |
) | |
func handleProcessingStatus(tx *gorm.DB, c *Record) { | |
processing := tx.Statement.Changed("ProcessinStatus") && | |
c.ProcessingStatus == ProcessingStatusPENDING | |
if !processing { | |
return | |
} | |
tx.Statement.SetColumn("requested_to_process_at", gorm.Expr("NOW()")) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package update | |
import ( | |
"github.com/simiancreative/simiango/server" | |
"github.com/simiancreative/simiango/service" | |
"api/lib/auth" | |
"api/lib/repositories/contracts" | |
"api/lib/req" | |
"api/lib/validator" | |
) | |
var Config = service.Config{ | |
Kind: service.DIRECT, | |
Method: "PATCH", | |
Path: "/contracts/:id", | |
Direct: Handler, | |
IsPrivate: true, | |
Auth: auth.AuthenticateUser, | |
Input: func() interface{} { return &Request{} }, | |
} | |
type Request struct { | |
ProcessingStatus string `json:"processing_status"` | |
// add properties as needed that the user can update | |
// keeps private properties private | |
} | |
func (u *Request) Patch(id string) error { | |
c := contracts.Record{ | |
ProcessingStatus: u.ProcessingStatus, | |
// add properties as needed that the user can update | |
// keeps private properties private | |
} | |
return contracts.Patch(id, c) | |
} | |
func Handler(request service.Req) (interface{}, error) { | |
body := request.Input.(*Request) | |
if err := validator.Validate(body); err != nil { | |
return nil, Error.Result( | |
ValidationFailed, | |
req.Reason{"errors": err}, | |
) | |
} | |
id, _ := request.Params.Get("id") | |
err := body.Patch(id.Value) | |
return nil, err | |
} | |
func init() { | |
server.AddService(Config) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment