github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section3/gopherfaceform/models/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 embeded into types we want to keep a 26 // check on for auditing purposes 27 type AuditableContent struct { 28 TimeCreated time.Time `json:"timeCreated"` 29 TimeModified time.Time `json:"timeModified"` 30 CreatedBy string `json:"createdBy"` 31 ModifiedBy string `json:"modifiedBy"` 32 } 33 34 // Post represents a Social Media Post type. 35 type Post struct { 36 AuditableContent // Embedded type 37 Caption string `json:"caption"` 38 MessageBody string `json:"messageBody"` 39 URL string `json:"url"` 40 ImageURI string `json:"imageURI"` 41 ThumbnailURI string `json:"thumbnailURI"` 42 Keywords []string `json:"keywords"` 43 Likers []string `json:"likers"` 44 AuthorMood MoodState `json:"authorMood"` 45 AuthorMoodEmoji string `json:authorMoodEmoji"` 46 } 47 48 // Map that holds the various mood states with keys to serve as 49 // aliases to their respective mood states. 50 var Moods map[string]MoodState 51 var MoodsEmoji map[MoodState]string 52 53 // The init() function is responsible for initializing the mood state 54 func init() { 55 Moods = map[string]MoodState{"neutral": MoodStateNeutral, "happy": MoodStateHappy, "sad": MoodStateSad, "angry": MoodStateAngry, "hopeful": MoodStateHopeful, "thrilled": MoodStateThrilled, "bored": MoodStateBored, "shy": MoodStateShy, "comical": MoodStateComical, "cloudnine": MoodStateOnCloudNine} 56 57 MoodsEmoji = map[MoodState]string{MoodStateNeutral: "\xF0\x9F\x98\x90", MoodStateHappy: "\xF0\x9F\x98\x8A", MoodStateSad: "\xF0\x9F\x98\x9E", MoodStateAngry: "\xF0\x9F\x98\xA0", MoodStateHopeful: "\xF0\x9F\x98\x8C", MoodStateThrilled: "\xF0\x9F\x98\x81", MoodStateBored: "\xF0\x9F\x98\xB4", MoodStateShy: "\xF0\x9F\x98\xB3", MoodStateComical: "\xF0\x9F\x98\x9C", MoodStateOnCloudNine: "\xF0\x9F\x98\x82"} 58 59 } 60 61 // NewPost is responsible for creating an instance of the Post type. 62 func NewPost(username string, mood MoodState, caption string, messageBody string, url string, imageURI string, thumbnailURI string, keywords []string) *Post { 63 64 auditableContent := AuditableContent{CreatedBy: username, TimeCreated: time.Now()} 65 return &Post{Caption: caption, MessageBody: messageBody, URL: url, ImageURI: imageURI, ThumbnailURI: thumbnailURI, AuthorMood: mood, Keywords: keywords, AuditableContent: auditableContent, AuthorMoodEmoji: MoodsEmoji[mood]} 66 }