"use strict";(self.webpackChunkgoffee_site=self.webpackChunkgoffee_site||[]).push([[7349],{8273:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>i,default:()=>l,frontMatter:()=>r,metadata:()=>a,toc:()=>g});var s=t(4848),o=t(8453);const r={title:"Events"},i=void 0,a={id:"events",title:"Events",description:"In Goffee defining events is straightforward, first you need to add the event name as a constant to the file events\\event-names.go",source:"@site/docs/events.md",sourceDirName:".",slug:"/events",permalink:"/goffee/docs/events",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{title:"Events"},sidebar:"docsSidebar",previous:{title:"Sending Emails",permalink:"/goffee/docs/emails"},next:{title:"Database Configuration",permalink:"/goffee/docs/database-config"}},c={},g=[{value:"Event Job",id:"event-job",level:3},{value:"Triggering an event",id:"triggering-an-event",level:3}];function d(e){const n={code:"code",h3:"h3",p:"p",pre:"pre",...(0,o.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.p,{children:["In ",(0,s.jsx)(n.code,{children:"Goffee"})," defining events is straightforward, first you need to add the event name as a constant to the file ",(0,s.jsx)(n.code,{children:"events\\event-names.go"})]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",metastring:'title="#file: events\\event-names.go"',children:'package events\n\nconst PASSWORD_CHANGED = "password-changed"\nconst PROCESS_COMPLETED = "process-completed"\n'})}),"\n",(0,s.jsxs)(n.p,{children:["Next, you need to create the ",(0,s.jsx)(n.code,{children:"event job"})]}),"\n",(0,s.jsx)(n.h3,{id:"event-job",children:"Event Job"}),"\n",(0,s.jsx)(n.p,{children:"the event job is simply a function that gets executed every time the event is triggered,\nhere is an example of one"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",metastring:'title="#file: events\\jobs"',children:'package eventjobs\n\nimport (\n    "fmt"\n\n    "git.smarteching.com/goffee/core"\n    "git.smarteching.com/goffee/goffee/models"\n)\n\nvar SendPasswordChangedNotificationEmail core.EventJob = func(event *core.Event, c *core.Context) {\n    go func() {\n        // logic to send the email goes here\n    }()\n}\n'})}),"\n",(0,s.jsxs)(n.p,{children:["Next, you have to register the ",(0,s.jsx)(n.code,{children:"event job"})," using the ",(0,s.jsx)(n.code,{children:"events manager"})," so it can be executed when the event is triggered, you can simply do that in the file ",(0,s.jsx)(n.code,{children:"register-events.go"})]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'package main\n\nimport (\n    "git.smarteching.com/goffee/core"\n    "git.smarteching.com/goffee/goffee/events"\n    eventjobs "git.smarteching.com/goffee/goffee/events/jobs"\n)\n\n// Register events\nfunc registerEvents() {\n    eventsManager := core.ResolveEventsManager()\n    //########################################\n    //#      events registration         #####\n    //########################################\n\n    // register your event here...\n    eventsManager.Register(events.PASSWORD_CHANGED, eventjobs.SendPasswordChangedNotificationEmail)\n}\n'})}),"\n",(0,s.jsx)(n.h3,{id:"triggering-an-event",children:"Triggering an event"}),"\n",(0,s.jsxs)(n.p,{children:["Events can be triggered using the ",(0,s.jsx)(n.code,{children:"events manager"}),", here is how"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n    "git.smarteching.com/goffee/goffee/events"\n    "git.smarteching.com/goffee/goffee/models"\n)\n\nfunc Signup(c *core.Context) *core.Response {\n    var user models.User\n    err = c.GetEventsManager().Fire(\n        &core.Event{\n            Name: events.PASSWORD_CHANGED, \n            Payload: map[string]interface{}{\n                "user": user,\n            }\n    })\n}\n'})})]})}function l(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>i,x:()=>a});var s=t(6540);const o={},r=s.createContext(o);function i(e){const n=s.useContext(r);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:i(e.components),s.createElement(r.Provider,{value:n},e.children)}}}]);