fix: improvement log record ads tih ip, useragent, and uer_id
This commit is contained in:
parent
b0099dac1e
commit
290d7f6701
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@ bin
|
||||
.env
|
||||
.DS_Store
|
||||
/cmd/legalgo/env
|
||||
|
||||
__debug*
|
||||
|
||||
@ -4,8 +4,10 @@ import "time"
|
||||
|
||||
type LogAds struct {
|
||||
ID string `gorm:"primaryKey;not null" json:"id"`
|
||||
AdsID string `gorm:"not null" json:"ads_id"`
|
||||
// UserID string `gorm:"default:null" json:"user_id"`
|
||||
ContentID string `gorm:"not null" json:"content_id"`
|
||||
UserID string `gorm:"default:null" json:"user_id"`
|
||||
IP string `gorm:"default:null" json:"ip"`
|
||||
UserAgent string `gorm:"default:null" json:"user_agent"`
|
||||
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
|
||||
}
|
||||
|
||||
@ -9,8 +9,8 @@ func (a *accessor) GetAll() ([]adsdomain.AdsResponse, error) {
|
||||
var ads []adsdomain.AdsResponse
|
||||
|
||||
if err := a.db.Table("ads").
|
||||
Select("ads.*, COUNT(log_ads.ads_id) as clicked").
|
||||
Joins("LEFT JOIN log_ads ON log_ads.ads_id = ads.id").
|
||||
Select("ads.*, COUNT(log_ads.content_id) as clicked").
|
||||
Joins("LEFT JOIN log_ads ON log_ads.content_id = ads.id").
|
||||
Group("ads.id").
|
||||
Scan(&ads).Error; err != nil {
|
||||
return ads, fmt.Errorf("failed to get all ads: %v", err)
|
||||
|
||||
@ -2,16 +2,29 @@ package logrepository
|
||||
|
||||
import (
|
||||
"legalgo-BE-go/database"
|
||||
logsdomain "legalgo-BE-go/internal/domain/logs"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (a *accessor) CreateLogAds(adsID string) error {
|
||||
spec := database.LogAds{
|
||||
func (a *accessor) CreateLogAds(spec logsdomain.LogsSpec) error {
|
||||
newSpec := database.LogAds{
|
||||
ID: uuid.NewString(),
|
||||
AdsID: adsID,
|
||||
ContentID: spec.ContentID,
|
||||
}
|
||||
if err := a.db.Create(&spec).Error; err != nil {
|
||||
|
||||
if spec.UserID != nil {
|
||||
newSpec.UserID = *spec.UserID
|
||||
}
|
||||
|
||||
if spec.IP != nil {
|
||||
newSpec.IP = *spec.IP
|
||||
}
|
||||
|
||||
if spec.UserAgent != nil {
|
||||
newSpec.UserAgent = *spec.UserAgent
|
||||
}
|
||||
if err := a.db.Create(&newSpec).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ package logrepository
|
||||
import (
|
||||
"legalgo-BE-go/database"
|
||||
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||
logsdomain "legalgo-BE-go/internal/domain/logs"
|
||||
)
|
||||
|
||||
type accessor struct {
|
||||
@ -10,7 +11,7 @@ type accessor struct {
|
||||
}
|
||||
|
||||
type Log interface {
|
||||
CreateLogAds(string) error
|
||||
CreateLogAds(logsdomain.LogsSpec) error
|
||||
GetAllLogAds(string) ([]adsdomain.Ads, error)
|
||||
}
|
||||
|
||||
|
||||
@ -7,30 +7,22 @@ import (
|
||||
"legalgo-BE-go/internal/utilities/response"
|
||||
"legalgo-BE-go/internal/utilities/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func CreateLogAds(
|
||||
router chi.Router,
|
||||
userSvc usersvc.User,
|
||||
logsSvc logssvc.Log,
|
||||
validate *validator.Validate,
|
||||
) {
|
||||
router.Post("/logs/ads", func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
// userDetail, err := utils.GetTokenDetail(r)
|
||||
// if err != nil {
|
||||
// response.RespondJsonErrorWithCode(
|
||||
// ctx,
|
||||
// w,
|
||||
// err,
|
||||
// response.ErrUnauthorized.Code,
|
||||
// response.ErrUnauthorized.HttpCode,
|
||||
// "unauthorized",
|
||||
// )
|
||||
// return
|
||||
// }
|
||||
var specReq logsdomain.LogsSpec
|
||||
|
||||
var spec logsdomain.LogsRequest
|
||||
|
||||
@ -46,7 +38,41 @@ func CreateLogAds(
|
||||
return
|
||||
}
|
||||
|
||||
if err := logsSvc.CreateLogAds(spec.AdsID); err != nil {
|
||||
if err := validate.Struct(spec); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
err,
|
||||
response.ErrBadRequest.Code,
|
||||
response.ErrBadRequest.HttpCode,
|
||||
err.(validator.ValidationErrors).Error(),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
specReq.ContentID = spec.AdsID
|
||||
|
||||
userDetail, _ := utils.GetTokenDetail(r)
|
||||
|
||||
if userDetail.ID != "" {
|
||||
specReq.UserID = &userDetail.ID
|
||||
}
|
||||
|
||||
ip := r.RemoteAddr
|
||||
|
||||
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
|
||||
ip = strings.Split(forwarded, ",")[0]
|
||||
}
|
||||
|
||||
if ip != "" {
|
||||
specReq.IP = &ip
|
||||
}
|
||||
|
||||
if userAgent := r.UserAgent(); userAgent != "" {
|
||||
specReq.UserAgent = &userAgent
|
||||
}
|
||||
|
||||
if err := logsSvc.CreateLogAds(specReq); err != nil {
|
||||
response.RespondJsonErrorWithCode(
|
||||
ctx,
|
||||
w,
|
||||
|
||||
@ -8,6 +8,13 @@ type LogsRequest struct {
|
||||
AdsID string `json:"ads_id" validate:"required"`
|
||||
}
|
||||
|
||||
type LogsSpec struct {
|
||||
ContentID string `json:"content_id" validate:"required"`
|
||||
IP *string `json:"ip"`
|
||||
UserID *string `json:"user_id"`
|
||||
UserAgent *string `json:"user_agent"`
|
||||
}
|
||||
|
||||
type LogResponse struct {
|
||||
ID string `json:"id"`
|
||||
News []adsdomain.Ads `json:"news"`
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
package logssvc
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
logsdomain "legalgo-BE-go/internal/domain/logs"
|
||||
)
|
||||
|
||||
func (i *impl) CreateLogAds(adsID string) error {
|
||||
if err := i.logsRepo.CreateLogAds(adsID); err != nil {
|
||||
func (i *impl) CreateLogAds(spec logsdomain.LogsSpec) error {
|
||||
if err := i.logsRepo.CreateLogAds(spec); err != nil {
|
||||
return fmt.Errorf("failed to create ads log: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,7 @@ package logssvc
|
||||
import (
|
||||
logrepository "legalgo-BE-go/internal/accessor/log"
|
||||
adsdomain "legalgo-BE-go/internal/domain/ads"
|
||||
logsdomain "legalgo-BE-go/internal/domain/logs"
|
||||
)
|
||||
|
||||
type impl struct {
|
||||
@ -10,7 +11,7 @@ type impl struct {
|
||||
}
|
||||
|
||||
type Log interface {
|
||||
CreateLogAds(string) error
|
||||
CreateLogAds(logsdomain.LogsSpec) error
|
||||
GetAllLogAds(string) ([]adsdomain.Ads, error)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user