github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section5/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 // All possible mood states. 12 const ( 13 MoodStateNeutral MoodState = iota 14 MoodStateHappy 15 MoodStateSad 16 MoodStateAngry 17 MoodStateHopeful 18 MoodStateThrilled 19 MoodStateBored 20 MoodStateShy 21 MoodStateComical 22 MoodStateOnCloudNine 23 ) 24 25 // AuditableContent types are meant to be embedded into types we want to keep a 26 // check on for auditing purposes 27 type AuditableContent struct { 28 TimeCreated time.Time 29 TimeModified time.Time 30 CreatedBy string 31 ModifiedBy string 32 } 33 34 // Post represents a Social Media Post type. 35 type Post struct { 36 AuditableContent // Embedded type 37 Caption string 38 MessageBody string 39 URL string 40 ImageURI string 41 ThumbnailURI string 42 Keywords []string 43 Likers []string 44 AuthorMood MoodState 45 } 46 47 // Map that holds the various mood states with keys to serve as 48 // aliases to their respective mood states. 49 var Moods map[string]MoodState 50 51 // The init() function is responsible for initializing the mood state 52 func init() { 53 Moods = map[string]MoodState{"neutral": MoodStateNeutral, "happy": MoodStateHappy, "sad": MoodStateSad, "angry": MoodStateAngry, "hopeful": MoodStateHopeful, "thrilled": MoodStateThrilled, "bored": MoodStateBored, "shy": MoodStateShy, "comical": MoodStateComical, "cloudnine": MoodStateOnCloudNine} 54 } 55 56 // NewPost is responsible for creating an instance of the Post type. 57 func NewPost(username string, mood MoodState, caption string, messageBody string, url string, imageURI string, thumbnailURI string, keywords []string) *Post { 58 59 auditableContent := AuditableContent{CreatedBy: username, TimeCreated: time.Now()} 60 return &Post{Caption: caption, MessageBody: messageBody, URL: url, ImageURI: imageURI, ThumbnailURI: thumbnailURI, AuthorMood: mood, Keywords: keywords, AuditableContent: auditableContent} 61 }