Skip to content

Commit

Permalink
feat: add user point logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 7, 2024
1 parent 1456a61 commit 6b57dbf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
11 changes: 8 additions & 3 deletions client/app/app/users/[slug]/page.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { useState, useEffect } from 'react';
import { CalendarFold } from 'lucide-react';
import { CalendarFold, Trash } from 'lucide-react';
import { getUser, getUserPosts } from '@/lib/api/users';
import { Skeleton } from '@/components/ui/skeleton';
import { useToast } from '@/components/ui/use-toast';
Expand Down Expand Up @@ -60,14 +60,19 @@ export default function Page({ params }) {
</div>
<div>
<div className='text-sm mb-2'>{user.about}</div>
<div className='text-xs'>
<div className='text-zinc-400 flex items-center mb-0.5'>
<div className='flex items-center gap-2 text-xs text-zinc-400'>
<div className='flex'>
<CalendarFold className='w-4 h-4 mr-1' />
{new Date(user.created_at.T * 1000).toLocaleDateString(
'tr-TR',
{ year: 'numeric', month: 'long', day: 'numeric' }
)}
</div>
{' · '}
<div className='flex'>
<Trash className='w-4 h-4 mr-1' />
{user.points} çöp puanı
</div>
</div>
</div>
</>
Expand Down
4 changes: 2 additions & 2 deletions client/components/app/Post/LikeButton.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button } from '@/components/ui/button';
import { Trash } from 'lucide-react';
import { Heart } from 'lucide-react';
import { likePost } from '@/lib/api/posts';
import useAuthStore from '@/stores/auth';

Expand Down Expand Up @@ -31,7 +31,7 @@ export default function LikeButton({ post, setPost }) {
className='px-2 h-7 text-xs'
onClick={(e) => handleLike(e, isLiked)}
>
<Trash className='w-3 h-3 mr-2' />
<Heart className='w-3 h-3 mr-2' />
{post.likes ? post.likes.length : 0}
</Button>
);
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/posts/update_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ func UpdatePost(c *fiber.Ctx) error {
if body.Like != nil {
if *body.Like {
update["$addToSet"] = bson.M{"likes": user.ID}
if post.Author.ID != user.ID {
models.UpdateUser(post.Author.ID, bson.M{"$inc": bson.M{"points": 1}})
}
} else {
update["$pull"] = bson.M{"likes": user.ID}
if post.Author.ID != user.ID {
models.UpdateUser(post.Author.ID, bson.M{"$inc": bson.M{"points": -1}})
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions server/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type User struct {
Username string `bson:"username" json:"username"`
Password string `bson:"password,omitempty" json:"password,omitempty"`
About string `bson:"about" json:"about,omitempty"`
Points int `bson:"points" json:"points,omitempty"`
}

type AuthStatus struct {
Expand Down Expand Up @@ -82,6 +83,7 @@ func CreateUser(user User) error {
user.IsBanned = false
user.IsAdmin = false
user.About = ""
user.Points = 1

_, err := database.Users.InsertOne(context.Background(), user)
if err != nil {
Expand All @@ -90,3 +92,12 @@ func CreateUser(user User) error {

return nil
}

func UpdateUser(userID primitive.ObjectID, update bson.M) error {
_, err := database.Users.UpdateOne(context.Background(), bson.M{"_id": userID}, update)
if err != nil {
return fmt.Errorf("error updating user: %v", err)
}

return nil
}

0 comments on commit 6b57dbf

Please sign in to comment.