Skip to content

Commit

Permalink
feat(master): update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thaisonenouvo committed Nov 28, 2023
1 parent 377ac9e commit eed28c3
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 62 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@

![system-design](docs/system-design.png)

## ERD Diagram

![erd-diagram](docs/erd.png)

## Start project

```
Expand Down
3 changes: 1 addition & 2 deletions apps/api/db/connectDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func ConnectDB() {
dsn := fmt.Sprintf("%s&parseTime=True", os.Getenv("DSN"))

DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
TranslateError: true,
DisableForeignKeyConstraintWhenMigrating: true,
TranslateError: true,
})
if err != nil {
log.Fatal("failed to open db connection", err)
Expand Down
2 changes: 2 additions & 0 deletions apps/api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (
gorm.io/gorm v1.25.5
)

require github.com/google/uuid v1.4.0 // indirect

require (
github.com/aws/aws-sdk-go-v2/credentials v1.16.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions apps/api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
Expand Down
5 changes: 4 additions & 1 deletion apps/api/handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func SignIn(c *gin.Context) {

user, err := services.GetUserByMail(req.Email)

if err != nil {
if err != nil && err == gorm.ErrRecordNotFound {
h.Fail400(c, "User doesn't exist")
return
} else if err != nil {
h.Fail400(c, err.Error())
return
}
Expand Down
4 changes: 1 addition & 3 deletions apps/api/handlers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ func CreateChat(c *gin.Context) {
return
}

user := c.MustGet("user").(*models.User)

chat, err := services.CreateChat(models.Chat{
Name: req.Name,
OwnerID: user.ID,
OwnerID: c.MustGet("user").(*models.User).ID,
})

if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions apps/api/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ func SendMessage(c *gin.Context) {
return
}

chatID, err := strconv.ParseUint(c.Param("chatID"), 10, 64)

message, err := services.CreateMessage(models.Message{
Content: req.Content,
ChatID: chatID,
ChatID: c.Param("chatID"),
OwnerID: c.MustGet("user").(*models.User).ID,
})

Expand All @@ -54,7 +52,7 @@ func SendMessage(c *gin.Context) {
return
}

err = services.UpdateLastMessage(chatID, *message)
err = services.UpdateLastMessage(c.Param("chatID"), *message)

if err != nil {
h.Fail400(c, err.Error())
Expand Down
8 changes: 7 additions & 1 deletion apps/api/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ func init() {
}

func main() {
db.DB.AutoMigrate(&models.User{}, &models.Chat{}, &models.Message{}, &models.FriendRequest{}, &models.Community{})
db.DB.AutoMigrate(
&models.User{},
&models.Community{},
&models.Chat{},
&models.Message{},
&models.FriendRequest{},
)
fmt.Println("👍 Migration complete")
}
14 changes: 12 additions & 2 deletions apps/api/models/base.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package models

import "time"
import (
"time"

"github.com/google/uuid"
"gorm.io/gorm"
)

type Base struct {
ID uint `gorm:"primary_key" json:"id"`
ID string `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `sql:"index" json:"deletedAt"`
}

func (base *Base) BeforeCreate(tx *gorm.DB) (err error) {
base.ID = uuid.New().String()
return nil
}

type BasePaginationRequest struct {
Page int `json:"page"`
Limit int `json:"limit"`
Expand Down
10 changes: 6 additions & 4 deletions apps/api/models/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package models

type Chat struct {
Base
Name string `gorm:"not null;type:varchar(255)" json:"name"`
Members []*User `gorm:"many2many:chat_users;" json:"members"`
OwnerID uint `gorm:"not null" json:"ownerId"`
LastMessage Message `gorm:"foreignKey:ChatID" json:"lastMessage"`
Name string `gorm:"not null;type:varchar(255)" json:"name"`
Members []*User `gorm:"many2many:chat_users;" json:"members"`
OwnerID string `gorm:"not null" json:"ownerId"`
Owner User `gorm:"foreignKey:OwnerID;references:ID" json:"owner"`
LastMessageID string `json:"lastMessageId"`
LastMessage *Message `gorm:"foreignKey:LastMessageID;references:ID" json:"lastMessage"`
}

type CreateChatRequest struct {
Expand Down
9 changes: 5 additions & 4 deletions apps/api/models/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package models

type Community struct {
Base
Name string `gorm:"not null;type:varchar(255)" json:"name"`
Logo string `gorm:"not null;type:text;" json:"logo"`
OwnerID uint `gorm:"not null" json:"ownerId"`
Users []User `gorm:"many2many:user_communities;" json:"users"`
Name string `gorm:"not null;type:varchar(255)" json:"name"`
Logo string `gorm:"not null;type:text;" json:"logo"`
OwnerID string `gorm:"not null" json:"ownerId"`
Owner *User `gorm:"foreignKey:OwnerID;references:ID" json:"owner"`
Users []*User `gorm:"many2many:user_communities;" json:"users"`
}

type CreateCommunityRequest struct {
Expand Down
8 changes: 4 additions & 4 deletions apps/api/models/friend-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package models

type FriendRequest struct {
Base
UserID uint `gorm:"not null" json:"userId"`
FriendID uint `gorm:"not null" json:"friendId"`
UserID string `gorm:"not null" json:"userId"`
FriendID string `gorm:"not null" json:"friendId"`
}

type AddFriendRequest struct {
UserID uint `json:"userId" validate:"required"`
FriendID uint `json:"friendId" validate:"required"`
UserID string `json:"userId" validate:"required"`
FriendID string `json:"friendId" validate:"required"`
}

type AcceptFriendRequest struct {
Expand Down
6 changes: 4 additions & 2 deletions apps/api/models/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package models
type Message struct {
Base
Content string `gorm:"not null;type:varchar(255)" json:"content"`
ChatID uint64 `gorm:"not null" json:"chatId"`
OwnerID uint `gorm:"not null" json:"ownerId"`
ChatID string `gorm:"not null" json:"chatId"`
Chat Chat `gorm:"foreignKey:ChatID;references:ID" json:"chat"`
OwnerID string `gorm:"not null;type:uuid" json:"ownerId"`
Owner User `gorm:"foreignKey:OwnerID;references:ID" json:"owner"`
}

type MessageRequest struct {
Expand Down
4 changes: 2 additions & 2 deletions apps/api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type User struct {
Password string `gorm:"not null" json:"-"`
Gender string `gorm:"not null" json:"gender"`
Chats []*Chat `gorm:"many2many:chat_users;" json:"chats"`
Communities []Community `gorm:"many2many:user_communities;" json:"communities"`
Communities []*Community `gorm:"many2many:user_communities;" json:"communities"`
Friends []*User `gorm:"many2many:user_friends" json:"friends"`
FriendRequests []FriendRequest `json:"friendRequests"`
FriendRequests []FriendRequest `gorm:"foreignKey:UserID" json:"friendRequests"`
}

type UserFriend struct {
Expand Down
8 changes: 4 additions & 4 deletions apps/api/services/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"github.com/tranthaison1231/meta-clone/api/models"
)

func GetChats(userID uint) (*[]models.Chat, error) {
func GetChats(userID string) (*[]models.Chat, error) {
var chats []models.Chat
err := db.DB.Model(&models.Chat{}).Preload("LastMessage").Preload("Members").Where("owner_id = ?", userID).Find(&chats).Error
err := db.DB.Model(&models.Chat{}).Preload("LastMessage").Preload("Members").Preload("Owner").Where("owner_id = ?", userID).Find(&chats).Error

if err != nil {
return nil, err
Expand Down Expand Up @@ -48,11 +48,11 @@ func AddMemberToChat(chatID uint64, memberID uint) (*models.Chat, error) {
return &chat, nil
}

func UpdateLastMessage(chatID uint64, message models.Message) error {
func UpdateLastMessage(chatID string, message models.Message) error {
var chat models.Chat
err := db.DB.Model(&chat).Where("id = ?", chatID).First(&chat).Error

chat.LastMessage = message
chat.LastMessageID = message.ID

err = db.DB.Save(&chat).Error

Expand Down
2 changes: 1 addition & 1 deletion apps/api/services/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/tranthaison1231/meta-clone/api/models"
)

func GetCommunities(userID uint) (*[]models.Community, error) {
func GetCommunities(userID string) (*[]models.Community, error) {
var communities []models.Community
err := db.DB.Model(&models.Community{}).Where("owner_id = ?", userID).Find(&communities).Error

Expand Down
4 changes: 2 additions & 2 deletions apps/api/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func GetUserFriends(userId uint, request *models.BasePaginationRequest) (*models
}, nil
}

func AddFriend(userId uint, friendId uint) (*models.FriendRequest, error) {
func AddFriend(userId string, friendId string) (*models.FriendRequest, error) {
var friendRequest models.FriendRequest
var user models.User

Expand Down Expand Up @@ -132,7 +132,7 @@ func AddFriend(userId uint, friendId uint) (*models.FriendRequest, error) {
return &newFriendRequest, nil
}

func AcceptFriend(userId uint, friendId uint, isRejecting bool) (string, error) {
func AcceptFriend(userId string, friendId string, isRejecting bool) (string, error) {
var friendRequest models.FriendRequest
var user models.User
var friend models.User
Expand Down
10 changes: 4 additions & 6 deletions apps/messenger-web/src/routes/(auth)/t/[id]/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script>
import { BellOff, MonitorDown } from 'lucide-svelte';
import MoreHorizontalModal from './MoreHorizontalModal.svelte';
import { useQuery } from '@sveltestack/svelte-query';
import { chatsApi } from '$lib/apis/chats';
import { twMerge } from 'tailwind-merge';
import MoreHorizontalModal from './MoreHorizontalModal.svelte';
const CONTACTS_ONLINE = [
{
Expand Down Expand Up @@ -131,7 +129,7 @@
];
</script>

<div class="border-r-1 w-90 relative h-screen py-2 transition-all max-lg:w-20">
<div class="relative h-screen w-90 border-r-1 py-2 transition-all max-lg:w-20">
<div class="px-4 max-lg:hidden">
<h1 class="my-4 mb-7 text-2xl font-bold">Chats</h1>
<input class="input border-none bg-[#F5F5F5] outline-none" placeholder="Search (Ctrl + K)" />
Expand Down Expand Up @@ -177,13 +175,13 @@
<MoreHorizontalModal />
<BellOff class="text-gray-500" size={20} />
{#if !isSeenMessage}
<div class="bg-primary h-3 w-3 rounded-full" />
<div class="h-3 w-3 rounded-full bg-primary" />
{/if}
</div>
</div>
{/each}
</div>
<div class="border-t-1 absolute bottom-0 w-full border px-2 py-3">
<div class="absolute bottom-0 w-full border border-t-1 px-2 py-3">
<button class="flex w-full justify-center gap-2 rounded-xl px-3 py-2 hover:bg-gray-100">
<MonitorDown />
Try Messenger for Mac
Expand Down
1 change: 0 additions & 1 deletion apps/messenger-web/src/routes/(auth)/t/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
Video
} from 'lucide-svelte';
import ReactMessageModal from './ReactMessageModal.svelte';
import MoreHorizontalModal from './MoreHorizontalModal.svelte';
const user = {
id: '2',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
<script>
import * as Popover from '$lib/components/ui/popover';
import { cn } from '$lib/utils/cn';
import {
AlertTriangle,
Archive,
BellOff,
Camera,
Check,
LogOut,
MoreHorizontal,
Phone,
Trash,
Video
} from 'lucide-svelte';
import {
angryIcon,
hahaIcon,
sadIcon,
loveIcon,
wowIcon,
heartIcon,
likeIcon,
heartIcon
loveIcon,
sadIcon,
wowIcon
} from '$lib/assets/svgs';
import * as Popover from '$lib/components/ui/popover';
import { MoreHorizontal } from 'lucide-svelte';
</script>

<Popover.Root>
Expand All @@ -30,7 +18,7 @@
<MoreHorizontal />
</div>
</Popover.Trigger>
<Popover.Content class="w-90 relative h-14 items-center px-2 py-1">
<Popover.Content class="relative h-14 w-90 items-center px-2 py-1">
<div class="flex items-start justify-between gap-2">
<img
src={hahaIcon}
Expand Down
Loading

1 comment on commit eed28c3

@vercel
Copy link

@vercel vercel bot commented on eed28c3 Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.