Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Jan 3, 2024
2 parents 2fcac34 + c9f54fc commit 21bde38
Show file tree
Hide file tree
Showing 26 changed files with 566 additions and 953 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ WORKDIR /app

COPY --from=builder /app .

RUN apt update
RUN apt install -y curl

ENTRYPOINT /app/poenskelisten -port ${port} -timezone ${timezone} -generateinvite ${generateinvite} -dbip ${dbip} -dbport ${dbport} -dbname ${dbname} -dbusername ${dbusername} -dbpassword ${dbpassword} -disablesmtp ${disablesmtp} -smtphost ${smtphost} -smtpport ${smtpport} -smtpusername ${smtpusername} -smtppassword ${smtppassword}
3 changes: 2 additions & 1 deletion controllers/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func APIUpdateCurrency(context *gin.Context) {
var currency models.UpdateCurrencyrequest

if err := context.ShouldBindJSON(&currency); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."})
context.Abort()
return
}
Expand Down
154 changes: 104 additions & 50 deletions controllers/groupcontroller.go → controllers/group.go

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions controllers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"aunefyren/poenskelisten/database"
"aunefyren/poenskelisten/middlewares"
"bytes"
"encoding/base64"
"errors"
Expand Down Expand Up @@ -98,11 +99,9 @@ func APIGetUserProfileImage(context *gin.Context) {

// Reply
context.JSON(http.StatusOK, gin.H{"image": base64, "default": defaultImage, "message": "Picture retrieved."})

}

func CheckIfWishImageExists(wishID uuid.UUID) (bool, error) {

var filePath = wish_image_path + "/" + wishID.String() + ".jpg"

_, err := LoadImageFile(filePath)
Expand All @@ -112,23 +111,19 @@ func CheckIfWishImageExists(wishID uuid.UUID) (bool, error) {
}

return true, nil

}

func LoadImageFile(filePath string) ([]byte, error) {

// Read the entire file into a byte slice
imageBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, errors.New("Failed to read file.")
}

return imageBytes, nil

}

func SaveImageFile(filePath string, fileName string, imageFile image.Image) error {

err := os.MkdirAll(filePath, 0755)
if err != nil {
log.Println("Failed to create directory for image. Error: " + err.Error())
Expand All @@ -147,11 +142,9 @@ func SaveImageFile(filePath string, fileName string, imageFile image.Image) erro
}

return nil

}

func ImageBytesToBase64(image []byte) (string, error) {

var base64Encoding string

// Determine the content type of the image file
Expand All @@ -174,11 +167,9 @@ func ImageBytesToBase64(image []byte) (string, error) {
base64Encoding += base64.StdEncoding.EncodeToString(image)

return base64Encoding, nil

}

func Base64ToImageBytes(base64String string) ([]byte, string, error) {

var imageBytes []byte
var b64Data string
var mimeType string
Expand All @@ -203,23 +194,19 @@ func Base64ToImageBytes(base64String string) ([]byte, string, error) {
}

return imageBytes, mimeType, nil

}

func LoadDefaultProfileImage() ([]byte, error) {

imageBytes, err := LoadImageFile(default_profile_image_path)
if err != nil {
log.Println("Failed to load default profile image. Error: " + err.Error() + ". Returning.")
return nil, errors.New("Failed to load default profile image.")
}

return imageBytes, nil

}

func ResizeImage(maxWidth uint, maxHeight uint, imageBytes []byte) ([]byte, error) {

// decode jpeg into image.Image
img, _, err := image.Decode(bytes.NewReader(imageBytes))
if err != nil {
Expand All @@ -243,7 +230,6 @@ func ResizeImage(maxWidth uint, maxHeight uint, imageBytes []byte) ([]byte, erro
}

func UpdateUserProfileImage(userID uuid.UUID, base64String string) error {

imageBytes, mimeType, err := Base64ToImageBytes(base64String)
if err != nil {
log.Println("Failed to convert Base64 String to bytes. Error: " + err.Error())
Expand Down Expand Up @@ -286,11 +272,9 @@ func UpdateUserProfileImage(userID uuid.UUID, base64String string) error {
}

return nil

}

func APIGetWishImage(context *gin.Context) {

// Create user request
var wishIDString = context.Param("wish_id")
var thumbnail = context.Query("thumbnail")
Expand All @@ -314,6 +298,27 @@ func APIGetWishImage(context *gin.Context) {
return
}

// Get wishlist object
wishlistFound, wishlist, err := database.GetWishlistByWishID(wishID)
if err != nil {
log.Println("Failed to get wishlist. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get wishlist."})
context.Abort()
return
} else if !wishlistFound {
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to find wishlist for wish."})
context.Abort()
return
} else if wishlist.Public != nil && !*wishlist.Public {
success, errorString, httpStatus := middlewares.AuthFunction(context, false)

if !success {
context.JSON(httpStatus, gin.H{"error": errorString})
context.Abort()
return
}
}

// Check if user exists
wishFound, _, err := database.GetWishByWishID(wishID)
if err != nil || !wishFound {
Expand Down Expand Up @@ -359,11 +364,9 @@ func APIGetWishImage(context *gin.Context) {

// Reply
context.JSON(http.StatusOK, gin.H{"image": base64, "message": "Picture retrieved."})

}

func SaveWishImage(wishID uuid.UUID, base64String string) error {

imageBytes, mimeType, err := Base64ToImageBytes(base64String)
if err != nil {
log.Println("Failed to convert Base64 String to bytes. Error: " + err.Error())
Expand Down Expand Up @@ -406,5 +409,4 @@ func SaveWishImage(wishID uuid.UUID, base64String string) error {
}

return nil

}
14 changes: 13 additions & 1 deletion controllers/invitecontroller.go → controllers/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"aunefyren/poenskelisten/models"
"log"
"net/http"
"sort"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand Down Expand Up @@ -36,6 +37,11 @@ func RegisterInvite(context *gin.Context) {
return
}

// Sort invites by creation date
sort.Slice(inviteObjects, func(i, j int) bool {
return inviteObjects[j].CreatedAt.Before(inviteObjects[i].CreatedAt)
})

context.JSON(http.StatusCreated, gin.H{"message": "Invitation created.", "invitation": invite, "invites": inviteObjects})

}
Expand All @@ -48,7 +54,8 @@ func APIDeleteInvite(context *gin.Context) {
// Parse group id
inviteIDInt, err := uuid.Parse(inviteID)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."})
context.Abort()
return
}
Expand Down Expand Up @@ -112,6 +119,11 @@ func APIGetAllInvites(context *gin.Context) {
return
}

// Sort invites by creation date
sort.Slice(inviteObjects, func(i, j int) bool {
return inviteObjects[j].CreatedAt.Before(inviteObjects[i].CreatedAt)
})

context.JSON(http.StatusOK, gin.H{"message": "Invites retrieved.", "invites": inviteObjects})
}

Expand Down
34 changes: 24 additions & 10 deletions controllers/newscontroller.go → controllers/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
Expand All @@ -18,7 +19,8 @@ func GetNews(context *gin.Context) {
newsPosts, err := database.GetNewsPosts()
if err != nil {
// If there is an error getting the list of news, return an internal server error
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Println("Failed to get news. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get news."})
context.Abort()
return
}
Expand All @@ -33,7 +35,8 @@ func GetNewsPost(context *gin.Context) {

newsIDInt, err := strconv.Atoi(newsID)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed parse request."})
context.Abort()
return
}
Expand All @@ -42,7 +45,8 @@ func GetNewsPost(context *gin.Context) {
newsPost, err := database.GetNewsPostByNewsID(newsIDInt)
if err != nil {
// If there is an error getting the news, return an internal server error
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Println("Failed to get news post. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed get news post."})
context.Abort()
return
}
Expand All @@ -60,11 +64,15 @@ func RegisterNewsPost(context *gin.Context) {
// Bind the incoming request body to the NewsCreationRequest model
if err := context.ShouldBindJSON(&newsCreationRequest); err != nil {
// If there is an error binding the request, return a Bad Request response
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."})
context.Abort()
return
}

newsCreationRequest.Title = strings.TrimSpace(newsCreationRequest.Title)
newsCreationRequest.Body = strings.TrimSpace(newsCreationRequest.Body)

// Copy the data from the NewsCreationRequest model to the News model
news.Title = newsCreationRequest.Title
news.Body = newsCreationRequest.Body
Expand Down Expand Up @@ -117,15 +125,17 @@ func RegisterNewsPost(context *gin.Context) {
newsRecord := database.Instance.Create(&news)
if newsRecord.Error != nil {
// If there is an error creating the news, return an Internal Server Error response
context.JSON(http.StatusInternalServerError, gin.H{"error": newsRecord.Error.Error()})
log.Println("Failed to create news post. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create news post."})
context.Abort()
return
}

newsPosts, err := database.GetNewsPosts()
if err != nil {
// If there is an error getting the list of news, return an internal server error
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Println("Failed to get news posts. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get news posts."})
context.Abort()
return
}
Expand All @@ -142,7 +152,8 @@ func DeleteNewsPost(context *gin.Context) {
// Parse news ID as integer
newsIDInt, err := strconv.Atoi(newsID)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."})
context.Abort()
return
}
Expand All @@ -151,15 +162,17 @@ func DeleteNewsPost(context *gin.Context) {
_, err = database.GetNewsPostByNewsID(newsIDInt)
if err != nil {
// If there is an error getting the news, return an internal server error
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Println("Failed to get news post. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get news post."})
context.Abort()
return
}

// Set the news post to disabled in the database
err = database.DeleteNewsPost(newsIDInt)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
log.Println("Failed to delete news post. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete news post."})
context.Abort()
return
}
Expand All @@ -168,7 +181,8 @@ func DeleteNewsPost(context *gin.Context) {
newsPosts, err := database.GetNewsPosts()
if err != nil {
// If there is an error getting the list of news, return an internal server error
context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Println("Failed to get news posts. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get news posts."})
context.Abort()
return
}
Expand Down
8 changes: 6 additions & 2 deletions controllers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ func APIGetServerInfo(context *gin.Context) {
}

serverInfo := models.ServerInfoReply{
Timezone: config.Timezone,
PoenskelistenVersion: config.PoenskelistenVersion,
Timezone: config.Timezone,
PoenskelistenVersion: config.PoenskelistenVersion,
PoenskelistenPort: config.PoenskelistenPort,
PoenskelistenExternalURL: config.PoenskelistenExternalURL,
DatabaseType: config.DBType,
SMTPEnabled: config.SMTPEnabled,
}

// Reply
Expand Down
6 changes: 3 additions & 3 deletions controllers/tokencontroller.go → controllers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ func GenerateToken(context *gin.Context) {
var request TokenRequest
var user models.User

if err := context.ShouldBindJSON(&request); err != nil {
err := context.ShouldBindJSON(&request)
if err != nil {
log.Println("Failed to parse request. Error: " + err.Error())
context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."})
context.Abort()
return
}

// check if email exists and password is correct
user, err := database.GetAllUserInformationByEmail(request.Email)
user, err = database.GetAllUserInformationByEmail(request.Email)
if err != nil {
log.Println("Failed to get user by e-mail. Error: " + err.Error())
context.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid credentials."})
Expand All @@ -54,7 +55,6 @@ func GenerateToken(context *gin.Context) {
}

context.JSON(http.StatusOK, gin.H{"token": tokenString, "message": "Logged in!"})

}

func ValidateToken(context *gin.Context) {
Expand Down
Loading

0 comments on commit 21bde38

Please sign in to comment.