Skip to content

Commit

Permalink
update profile image url to seocial_media_repo.go
Browse files Browse the repository at this point in the history
  • Loading branch information
devkuros committed Apr 7, 2022
1 parent 547631d commit d02c819
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions models/socialmedias_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type UserSocialMediaBody struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
StatsUsers struct {
ID uint `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
ID uint `json:"id"`
Username string `json:"username"`
ProfileImgUrl string `json:"profile_image_url"`
} `json:"user"`
} `json:"social_medias"`
}
Expand Down
16 changes: 13 additions & 3 deletions repositories/social_media_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (som *socialMediaRepo) GetSocialMedias(c *gin.Context) {

Social := models.SocialMedia{}
User := models.User{}
Photo := models.Photo{}
SocialBody := models.UserSocialMediaBody{}

userID := uint(userData["id"].(float64))
Expand All @@ -40,22 +41,31 @@ func (som *socialMediaRepo) GetSocialMedias(c *gin.Context) {
}

Social.UserID = userID
if err := som.DB.Debug().Find(&Social).Error; err != nil {

if err := som.DB.Where("user_id = ?", userID).First(&Social).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": err.Error(),
})
return
}

if err := som.DB.Debug().Where("id = ?", Social.UserID).First(&User).Error; err != nil {
if err := som.DB.Where("id = ?", Social.UserID).First(&User).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Bad Request",
"message": err.Error(),
})
return
}

if err := som.DB.Select("photo_url").Where("user_id = ?", userID).First(&Photo).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{
"error": "Data not found",
"message": err.Error(),
})
return
}

SocialBody.StatsSocialMedia.ID = Social.ID
SocialBody.StatsSocialMedia.Nama = Social.Nama
SocialBody.StatsSocialMedia.SocialMediaUrl = Social.SocialMediaUrl
Expand All @@ -65,7 +75,7 @@ func (som *socialMediaRepo) GetSocialMedias(c *gin.Context) {

SocialBody.StatsSocialMedia.StatsUsers.ID = User.ID
SocialBody.StatsSocialMedia.StatsUsers.Username = User.Username
SocialBody.StatsSocialMedia.StatsUsers.Email = User.Email
SocialBody.StatsSocialMedia.StatsUsers.ProfileImgUrl = Photo.PhotoUrl

c.JSON(http.StatusOK, SocialBody)
}
Expand Down

0 comments on commit d02c819

Please sign in to comment.