github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume3/section5/gopherface/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 TimeCreatedUnixTS int `json:"timeCreatedUnixTS"` 31 TimeModifiedUnixTS int `json:"timeModifiedUnixTS"` 32 CreatedBy string `json:"createdBy"` 33 ModifiedBy string `json:"modifiedBy"` 34 } 35 36 // Post represents a Social Media Post type. 37 type Post struct { 38 AuditableContent // Embedded type 39 UUID string `json:"uuid"` 40 RawMoodValue int `json:"rawMoodValue"` 41 Username string `json:"username"` 42 ProfileImagePath string `json: "profileImagePath"` 43 Caption string `json:"caption"` 44 MessageBody string `json:"messageBody"` 45 URL string `json:"url"` 46 ImageURI string `json:"imageURI"` 47 ThumbnailURI string `json:"thumbnailURI"` 48 Keywords []string `json:"keywords"` 49 Likers []string `json:"likers"` 50 AuthorMood MoodState `json:"authorMood"` 51 AuthorMoodEmoji string `json:authorMoodEmoji"` 52 } 53 54 // Map that holds the various mood states with keys to serve as 55 // aliases to their respective mood states. 56 var Moods map[string]MoodState 57 var MoodsEmoji map[MoodState]string 58 59 // The init() function is responsible for initializing the mood state 60 func init() { 61 Moods = map[string]MoodState{"neutral": MoodStateNeutral, "happy": MoodStateHappy, "sad": MoodStateSad, "angry": MoodStateAngry, "hopeful": MoodStateHopeful, "thrilled": MoodStateThrilled, "bored": MoodStateBored, "shy": MoodStateShy, "comical": MoodStateComical, "cloudnine": MoodStateOnCloudNine} 62 63 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"} 64 65 } 66 67 // NewPost is responsible for creating an instance of the Post type. 68 func NewPost(username string, mood MoodState, caption string, messageBody string, url string, imageURI string, thumbnailURI string, keywords []string) *Post { 69 70 auditableContent := AuditableContent{CreatedBy: username, TimeCreated: time.Now()} 71 return &Post{Caption: caption, MessageBody: messageBody, URL: url, ImageURI: imageURI, ThumbnailURI: thumbnailURI, AuthorMood: mood, Keywords: keywords, AuditableContent: auditableContent, AuthorMoodEmoji: MoodsEmoji[mood]} 72 }