forked from Capstone-Mikti/Capstone-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
go.sum database tree | ||
20857462 | ||
Kn0F1e8O8fQOl6YvfyegETTq1tmu1qpkfDZj5AtvhWI= | ||
|
||
— sum.golang.org Az3gru8zyaj7EuaczqV+hDOTF+63AeV8mvhZHqRv6bbiV3gaYIvN9DnNHoSzVwNBCGSXS7qp50Ui6l9wNpxiJmNpwAc= |
5 changes: 5 additions & 0 deletions
5
db/migration-golang/20231127065901_create_notification_tabel.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN; | ||
|
||
DROP TABLE IF EXISTS "public"."notification"; | ||
|
||
COMMIT; |
11 changes: 11 additions & 0 deletions
11
db/migration-golang/20231127065901_create_notification_tabel.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
BEGIN; | ||
CREATE TABLE IF NOT EXISTS "public"."notifications" ( | ||
ID SERIAL NOT NULL PRIMARY KEY, | ||
Type TEXT, | ||
Message TEXT, | ||
Is_Read BOOLEAN DEFAULT FALSE, | ||
Created_At TIMESTAMP, | ||
Updated_At TIMESTAMP, | ||
Deleted_At TIMESTAMP | ||
); | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package entity | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Notification struct { | ||
ID int `json:"id"` | ||
Type string `json:"type"` | ||
Message string `json:"message"` | ||
IsRead bool `json:"is_read"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
DeletedAt time.Time `json:"deleted_at"` | ||
} | ||
|
||
func NewNotification(id int, typ, message string, isRead bool, createdAt, updatedAt, deletedAt time.Time) *Notification { | ||
return &Notification{ | ||
ID: id, | ||
Type: typ, | ||
Message: message, | ||
IsRead: isRead, | ||
CreatedAt: createdAt, | ||
UpdatedAt: updatedAt, | ||
DeletedAt: deletedAt, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
"Ticketing/entity" | ||
"Ticketing/internal/service" | ||
|
||
"github.com/labstack/echo/v4" | ||
"time" | ||
"Ticketing/internal/http/validator" | ||
) | ||
|
||
type NotificationHandler struct { | ||
notificationService service.NotificationUsecase | ||
} | ||
|
||
func NewNotificationHandler(notificationService service.NotificationUsecase) *NotificationHandler { | ||
return &NotificationHandler{notificationService} | ||
} | ||
|
||
|
||
// GetAllNotification | ||
func (h *NotificationHandler) GetAllNotification(c echo.Context) error { | ||
Notifications, err := h.notificationService.GetAllNotification(c.Request().Context()) | ||
if err != nil { | ||
return c.JSON(http.StatusUnprocessableEntity, err) | ||
} | ||
return c.JSON(http.StatusOK, map[string]interface{}{ | ||
"data": Notifications, | ||
}) | ||
} | ||
|
||
|
||
// func untuk create notification | ||
func (h *NotificationHandler) CreateNotification(c echo.Context) error { | ||
var input struct { | ||
Type string `json:"type" validate:"required"` | ||
Message string `json:"message" validate:"required"` | ||
Is_Read bool `json:"is_read"` | ||
Create_at time.Time `json:"create_at"` | ||
} | ||
|
||
// Input validation | ||
if err := c.Bind(&input); err != nil { | ||
return c.JSON(http.StatusBadRequest, validator.ValidatorErrors(err)) | ||
} | ||
|
||
// Create a Notification object | ||
Notification := entity.Notification{ | ||
Type: input.Type, | ||
Message: input.Message, | ||
IsRead: input.Is_Read, | ||
CreatedAt: time.Now(), | ||
} | ||
|
||
err := h.notificationService.CreateNotification(c.Request().Context(), &Notification) | ||
if err != nil { | ||
return c.JSON(http.StatusInternalServerError, validator.ValidatorErrors(err)) | ||
} | ||
|
||
return c.JSON(http.StatusCreated, Notification) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"Ticketing/entity" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type NotificationRepository struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewNotificationRepository(db *gorm.DB) *NotificationRepository { | ||
return &NotificationRepository{ | ||
db: db, | ||
} | ||
} | ||
|
||
// get all notification | ||
func (r *NotificationRepository) GetAllNotification(ctx context.Context) ([]*entity.Notification, error) { | ||
Notifications := make([]*entity.Notification, 0) | ||
result := r.db.WithContext(ctx).Find(&Notifications) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return Notifications, nil | ||
} | ||
// create notification | ||
func (r *NotificationRepository) CreateNotification(ctx context.Context, Notification *entity.Notification) error { | ||
result := r.db.WithContext(ctx).Create(&Notification) | ||
if result.Error != nil { | ||
return result.Error | ||
} | ||
return nil | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package service | ||
|
||
import ( | ||
"Ticketing/entity" | ||
"context" | ||
) | ||
|
||
type NotificationUsecase interface { | ||
GetAllNotification(ctx context.Context) ([]*entity.Notification, error) | ||
CreateNotification(ctx context.Context, Notification *entity.Notification) error | ||
} | ||
|
||
type NotificationRepository interface { | ||
GetAllNotification(ctx context.Context) ([]*entity.Notification, error) | ||
CreateNotification(ctx context.Context, Notification *entity.Notification) error | ||
} | ||
|
||
type NotificationService struct { | ||
Repository NotificationRepository | ||
} | ||
|
||
func NewNotificationService(Repository NotificationRepository) *NotificationService { | ||
return &NotificationService{Repository: Repository} | ||
} | ||
|
||
// Get All Notification ketika di get maka status notifikasi akan berubah menjadi true | ||
func (s *NotificationService) GetAllNotification(ctx context.Context) ([]*entity.Notification, error) { | ||
return s.Repository.GetAllNotification(ctx) | ||
} | ||
|
||
// func untuk create notification | ||
func (s *NotificationService) CreateNotification(ctx context.Context, Notification *entity.Notification) error { | ||
return s.Repository.CreateNotification(ctx, Notification) | ||
} |