github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section3/socialmedia/socialmedia.go (about) 1 // Package SocialMedia implements common functionality needed for social media web applications. 2 package socialmedia 3 4 import ( 5 "time" 6 ) 7 8 //go:generate stringer -type=MoodState 9 type MoodState int 10 11 // Here we define all the possible mood states using an 12 // iota enumerator. 13 const ( 14 MoodStateNeutral MoodState = iota 15 MoodStateHappy 16 MoodStateSad 17 MoodStateAngry 18 MoodStateHopeful 19 MoodStateThrilled 20 MoodStateBored 21 MoodStateShy 22 MoodStateComical 23 MoodStateOnCloudNine 24 ) 25 26 // This is a type we embed into types we want to keep a 27 // check on for auditing purposes 28 type AuditableContent struct { 29 TimeCreated time.Time 30 TimeModified time.Time 31 CreatedBy string 32 ModifiedBy string 33 } 34 35 // This is the type that represents a Social Media Post 36 type Post struct { 37 AuditableContent // Embedded type 38 Caption string 39 MessageBody string 40 URL string 41 ImageURI string 42 ThumbnailURI string 43 Keywords []string 44 Likers []string 45 AuthorMood MoodState 46 } 47 48 // Map that holds the various mood states with keys to serve as 49 // aliases to their respective mood state 50 var Moods map[string]MoodState 51 52 // The init() function is responsible for initializing the mood state 53 func init() { 54 Moods = map[string]MoodState{"neutral": MoodStateNeutral, "happy": MoodStateHappy, "sad": MoodStateSad, "angry": MoodStateAngry, "hopeful": MoodStateHopeful, "thrilled": MoodStateThrilled, "bored": MoodStateBored, "shy": MoodStateShy, "comical": MoodStateComical, "cloudnine": MoodStateOnCloudNine} 55 } 56 57 // This is the function responsible for creating a new social media post. 58 func NewPost(username string, mood MoodState, caption string, messageBody string, url string, imageURI string, thumbnailURI string, keywords []string) *Post { 59 60 auditableContent := AuditableContent{CreatedBy: username, TimeCreated: time.Now()} 61 return &Post{Caption: caption, MessageBody: messageBody, URL: url, ImageURI: imageURI, ThumbnailURI: thumbnailURI, AuthorMood: mood, Keywords: keywords, AuditableContent: auditableContent} 62 }