Skip to content

Commit ebdabbf

Browse files
committed
Check if posts with image are present in subreddit
1 parent bba60fb commit ebdabbf

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

controllers/gimme/nRandomMemes.go

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ func (g Controller) GetNRandomMemes(c *gin.Context) {
4040
// Remove Non Image posts from the Array
4141
memes = utils.RemoveNonImagePosts(memes)
4242

43+
// Check if the Memes list has any posts
44+
if len(memes) == 0 {
45+
response := response.Error{
46+
Code: http.StatusInternalServerError,
47+
Message: "Error while getting Memes",
48+
}
49+
50+
c.JSON(http.StatusInternalServerError, response)
51+
return
52+
}
53+
4354
// Get N no. of posts from that list
4455
memes = utils.GetNRandomMemes(memes, count)
4556

controllers/gimme/nRandomPostsFromSub.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gimme
22

33
import (
4+
"fmt"
45
"net/http"
56
"strconv"
67

@@ -46,6 +47,17 @@ func (g Controller) GetNPostsFromSub(c *gin.Context) {
4647
// Remove Non Image posts from the Array
4748
memes = utils.RemoveNonImagePosts(memes)
4849

50+
// Check if the Memes list has any posts
51+
if len(memes) == 0 {
52+
response := response.Error{
53+
Code: http.StatusBadRequest,
54+
Message: fmt.Sprintf("r/%s has no Posts with Images", sub),
55+
}
56+
57+
c.JSON(http.StatusBadRequest, response)
58+
return
59+
}
60+
4961
// Get N no. of posts from that list
5062
memes = utils.GetNRandomMemes(memes, count)
5163

controllers/gimme/onePostFromSub.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gimme
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/R3l3ntl3ss/Meme_Api/controllers/utils"
@@ -30,6 +31,17 @@ func (g Controller) GetOnePostFromSub(c *gin.Context) {
3031
// Remove Non Image posts from the Array
3132
memes = utils.RemoveNonImagePosts(memes)
3233

34+
// Check if the Memes list has any posts
35+
if len(memes) == 0 {
36+
response := response.Error{
37+
Code: http.StatusBadRequest,
38+
Message: fmt.Sprintf("r/%s has no Posts with Images", sub),
39+
}
40+
41+
c.JSON(http.StatusBadRequest, response)
42+
return
43+
}
44+
3345
// Choose one post from the list
3446
meme := memes[utils.GetRandomN(len(memes))]
3547

controllers/gimme/oneRandomMeme.go

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ func (g Controller) GetOneRandomMeme(c *gin.Context) {
3333
// Remove Non Image posts from the Array
3434
memes = utils.RemoveNonImagePosts(memes)
3535

36+
// Check if the Memes list has any posts
37+
if len(memes) == 0 {
38+
response := response.Error{
39+
Code: http.StatusInternalServerError,
40+
Message: "Error while getting Memes",
41+
}
42+
43+
c.JSON(http.StatusInternalServerError, response)
44+
return
45+
}
46+
3647
// Choose one post from the list
3748
meme := memes[utils.GetRandomN(len(memes))]
3849

0 commit comments

Comments
 (0)