package mapper import ( "github.com/rs/zerolog" "go-humas-be/app/database/entity" ppidDataCategoriesRepository "go-humas-be/app/module/ppid_data_categories/repository" ppidDataFilesMapper "go-humas-be/app/module/ppid_data_files/mapper" ppidDataFilesRepository "go-humas-be/app/module/ppid_data_files/repository" ppidDataFilesResponse "go-humas-be/app/module/ppid_data_files/response" res "go-humas-be/app/module/ppid_datas/response" usersRepository "go-humas-be/app/module/users/repository" "time" ) func PpidDatasResponseMapper( log zerolog.Logger, ppidDataCategoriesRepo ppidDataCategoriesRepository.PpidDataCategoriesRepository, ppidDataFilesRepo ppidDataFilesRepository.PpidDataFilesRepository, usersRepo usersRepository.UsersRepository, ppidDatasReq *entity.PpidDatas, ) (ppidDatasRes *res.PpidDatasResponse) { if ppidDatasReq == nil { return nil } //log.Info().Str("timestamp", time.Now(). // Format(time.RFC3339)).Str("Service:PpidDatasResponseMapper", "PpidData:Category"). // Interface("Category", ppidDatasReq.Category).Msg("") log.Info().Str("timestamp", time.Now(). Format(time.RFC3339)).Str("Service:PpidDatasResponseMapper", "PpidData:Category"). Interface("CreatedBy", ppidDatasReq.CreatedBy).Msg("") categoryName := "" if ppidDatasReq.Category.ID != 0 { categoryName = ppidDatasReq.Category.Title } findUser, err := usersRepo.FindOne(*ppidDatasReq.CreatedById) if err != nil { log.Error().Err(err).Msg("Error fetching user") return nil } createdByName := "" if findUser != nil { createdByName = findUser.Fullname } ppidDataFiles, err := ppidDataFilesRepo.FindByPpidData(ppidDatasReq.ID) if err != nil { log.Error().Err(err).Msg("Error fetching files") return nil } var ppidDataFilesArr []*ppidDataFilesResponse.PpidDataFilesResponse if len(ppidDataFiles) > 0 { for _, result := range ppidDataFiles { ppidDataFilesArr = append(ppidDataFilesArr, ppidDataFilesMapper.PpidDataFilesResponseMapper(result)) } } ppidDatasRes = &res.PpidDatasResponse{ ID: ppidDatasReq.ID, Title: ppidDatasReq.Title, Description: ppidDatasReq.Description, Slug: ppidDatasReq.Slug, CategoryId: ppidDatasReq.CategoryId, CategoryName: &categoryName, CreatedById: ppidDatasReq.CreatedById, CreatedByName: &createdByName, Position: ppidDatasReq.Position, StatusId: ppidDatasReq.StatusId, IsPublish: ppidDatasReq.IsPublish, PublishedAt: ppidDatasReq.PublishedAt, IsActive: ppidDatasReq.IsActive, CreatedAt: ppidDatasReq.CreatedAt, UpdatedAt: ppidDatasReq.UpdatedAt, PpidDataFiles: ppidDataFilesArr, } return ppidDatasRes }