Created
April 22, 2025 02:58
-
-
Save naiplawan/f8645c7a122902b34b9b23b94208779e to your computer and use it in GitHub Desktop.
Get Company by UUID
This file contains hidden or 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
| // GetByCompanyUUID godoc | |
| // @Summary Get a company by UUID | |
| // @Description Retrieve a single company by its UUID, including themes, employees, and users | |
| // @Tags admin | |
| // @Security BearerAuth | |
| // @Accept json | |
| // @Produce json | |
| // @Param company_uuid path string true "Company UUID" | |
| // @Success 200 {object} response.Response{data=dto.AdminCompanyManageDTOResponse} | |
| // @Failure 400 {object} response.Response | |
| // @Failure 404 {object} response.Response | |
| // @Failure 500 {object} response.Response | |
| // @Router /api/v1/admin/company/{company_uuid} [get] | |
| func (ch *adminCompanyManageHandler) GetByCompanyUUID(c *fiber.Ctx) error { | |
| // Parse the Company UUID from the path | |
| companyUUIDStr := c.Params("company_uuid") | |
| if companyUUIDStr == "" { | |
| return fiber.NewError(fiber.StatusBadRequest, "Company UUID is required") | |
| } | |
| companyUUID, err := uuid.Parse(companyUUIDStr) | |
| if err != nil { | |
| return fiber.NewError(fiber.StatusBadRequest, "Invalid Company UUID") | |
| } | |
| // Fetch the company using the usecase | |
| company, err := ch.adminCompanyManageUsecase.GetCompanyByUUID(companyUUID) | |
| if err != nil { | |
| if err == repository.ErrNotFound { | |
| return fiber.NewError(fiber.StatusNotFound, "Company not found") | |
| } | |
| return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | |
| } | |
| // Fetch themes, employees, and users related to the company | |
| themes, err := ch.adminCompanyManageUsecase.GetThemesByCompanyUUID(companyUUID) | |
| if err != nil { | |
| return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch themes: "+err.Error()) | |
| } | |
| employees, err := ch.companyEmployeeUsecase.GetEmployeesByCompanyUUID(companyUUID) | |
| if err != nil { | |
| return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch employees: "+err.Error()) | |
| } | |
| users, err := ch.userUsecase.GetUsersByCompanyUUID(companyUUID) | |
| if err != nil { | |
| return fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch users: "+err.Error()) | |
| } | |
| // Transform the company into a response DTO | |
| companyResponse := dto.AdminCompanyManageDTOResponse{ | |
| CompanyUUID: company.UUID, | |
| CompanyName: company.CompanyName, | |
| CompanyTaxNumber: company.TaxNumber, | |
| CompanyAddress: company.Address, | |
| CompanyWebsiteURL: utils.GetSafeString(company.WebsiteURL), | |
| CompanyMapURL: utils.GetSafeString(company.MapURL), | |
| CompanyFacebookURL: utils.GetSafeString(company.FacebookURL), | |
| CompanyInstagramURL: utils.GetSafeString(company.InstagramURL), | |
| CompanyLinkedinURL: utils.GetSafeString(company.LinkedinURL), | |
| CompanyTiktokURL: utils.GetSafeString(company.TiktokURL), | |
| CompanyYoutubeURL: utils.GetSafeString(company.YoutubeURL), | |
| CompanyLineURL: utils.GetSafeString(company.LineURL), | |
| CompanyLogoImage: utils.GetSafeString(company.LogoImage), | |
| CompanyStatus: string(company.License.Status), | |
| CompanyCreatedOn: company.CreatedOn, | |
| CompanyUpdatedOn: company.UpdatedOn, | |
| Themes: themes, // Add themes to the response | |
| Employees: employees, // Add employees to the response | |
| Users: users, // Add users to the response | |
| } | |
| if company.License != nil { | |
| companyResponse.License = *company.License | |
| } | |
| if company.License.Product != nil { | |
| companyResponse.Product = *company.License.Product | |
| } | |
| // Transform URLs if uploader is available | |
| if ch.uploader != nil { | |
| urlTransformer := httpUtils.NewModelURLTransformer(ch.uploader) | |
| urlTransformer.TransformURLs(&companyResponse) | |
| } else { | |
| log.Println("Warning: uploader is nil, using raw URLs without transformation") | |
| } | |
| // Return the success response | |
| return response.Success(c, "COMPANY_FOUND", "Company fetched successfully", companyResponse) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment