Skip to content

Commit

Permalink
update program push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
rezapace committed Nov 30, 2023
1 parent e1e306f commit 4362e45
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 4 deletions.
5 changes: 5 additions & 0 deletions %USERPROFILE%/go/bin/pkg/sumdb/sum.golang.org/latest
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=
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

DROP TABLE IF EXISTS "public"."notification";

COMMIT;
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;
27 changes: 27 additions & 0 deletions entity/notification.go
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,
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ require (
github.com/caarlos0/env/v6 v6.10.1
github.com/creasty/defaults v1.7.0
github.com/go-playground/validator/v10 v10.16.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/joho/godotenv v1.5.1
github.com/labstack/echo-jwt/v4 v4.2.0
github.com/labstack/echo/v4 v4.11.3
golang.org/x/crypto v0.14.0
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
)
Expand All @@ -18,7 +20,6 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
Expand All @@ -30,7 +31,6 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
6 changes: 5 additions & 1 deletion internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func BuildPrivateRoutes(cfg *config.Config, db *gorm.DB) []*router.Route {
OrderService := service.NewOrderService(OrderRepository)
OrderHandler := handler.NewOrderHandler(OrderService)

NotificationRepository := repository.NewNotificationRepository(db)
NotificationService := service.NewNotificationService(NotificationRepository)
NotificationHandler := handler.NewNotificationHandler(NotificationService)

// Menggunakan PrivateRoutes dengan kedua handler
return router.PrivateRoutes(userHandler, ticketHandler, BlogHandler, OrderHandler)
return router.PrivateRoutes(userHandler, ticketHandler, BlogHandler, OrderHandler, NotificationHandler)
}
65 changes: 65 additions & 0 deletions internal/http/handler/notification_hendler.go
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)
}



18 changes: 17 additions & 1 deletion internal/http/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func PublicRoutes(authHandler *handler.AuthHandler) []*Route {

// membuat fungsi untuk mengembalikan route
// pada func ini tdk perlu login krna public
func PrivateRoutes(UserHandler *handler.UserHandler, TicketHandler *handler.TicketHandler, BlogHandler *handler.BlogHandler, OrderHandler *handler.OrderHandler) []*Route {
func PrivateRoutes(UserHandler *handler.UserHandler, TicketHandler *handler.TicketHandler, BlogHandler *handler.BlogHandler, OrderHandler *handler.OrderHandler , NotificationHandler *handler.NotificationHandler) []*Route {
return []*Route{
{
Method: echo.POST,
Expand Down Expand Up @@ -262,6 +262,22 @@ func PrivateRoutes(UserHandler *handler.UserHandler, TicketHandler *handler.Tick
Handler: TicketHandler.SortTicketByAvailable,
Role: allRoles,
},

// create notification
{
Method: echo.POST,
Path: "/notification",
Handler: NotificationHandler.CreateNotification,
Role: allRoles,
},

// get all notification
{
Method: echo.GET,
Path: "/notification",
Handler: NotificationHandler.GetAllNotification,
Role: allRoles,
},
}
}

Expand Down
38 changes: 38 additions & 0 deletions internal/repository/notification_repository.go
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
}



34 changes: 34 additions & 0 deletions internal/service/notification_services.go
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)
}

0 comments on commit 4362e45

Please sign in to comment.