initial commit

This commit is contained in:
2026-05-25 11:52:26 +05:30
commit 0d42ac84e1
53 changed files with 11222 additions and 0 deletions

View File

@@ -0,0 +1,271 @@
package controllers
import (
"nearle/models"
"nearle/services"
"net/http"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
)
type CustomerController struct {
customerService services.CustomerService
}
func NewCustomerController(customerService services.CustomerService) *CustomerController {
return &CustomerController{customerService: customerService}
}
func (ctl *CustomerController) GetCustomer(c *fiber.Ctx) error {
cid, _ := strconv.Atoi(c.Query("customerid"))
cno := c.Query("contactno")
result, err := ctl.customerService.GetCustomer(cid, cno)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": err.Error(),
"status": false,
"details": fiber.Map{},
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *CustomerController) UpdateCustomer(c *fiber.Ctx) error {
var data models.Customers
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
result := ctl.customerService.UpdateCustomer(data)
if !result.Status {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": result.Message,
})
}
return c.Status(http.StatusAccepted).JSON(fiber.Map{
"status": true,
"code": http.StatusAccepted,
"message": result.Message,
})
}
func (ctl *CustomerController) GetCustomerLocations(c *fiber.Ctx) error {
cid, err := strconv.Atoi(c.Query("customerid"))
if err != nil || cid == 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"status": false,
"message": "Invalid customerid",
"details": []interface{}{},
})
}
result := ctl.customerService.GetCustomerLocations(cid)
return c.Status(result.Code).JSON(result)
}
func (ctl *CustomerController) CreateCustomerLocation(c *fiber.Ctx) error {
var data models.Customerlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
id, err := ctl.customerService.CreateCustomerLocation(data)
if id == -1 {
// Duplicate address
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": "Address already exists",
})
}
if err != nil || id == 0 {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": "Failed",
})
}
result := ctl.customerService.GetCustomerLocations(id)
return c.Status(result.Code).JSON(result)
}
func (ctl *CustomerController) CreateCustomerRequest(c *fiber.Ctx) error {
var req models.CustomerRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
now := time.Now()
req.Created = now
req.Updated = now
createdReq, err := ctl.customerService.CreateCustomerRequest(req)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": "Failed to create customer request",
"status": false,
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"code": http.StatusCreated,
"message": "Customer request created successfully",
"status": true,
"data": createdReq,
})
}
func (ctl *CustomerController) GetCustomerRequests(c *fiber.Ctx) error {
customerIDStr := c.Query("customerid")
pageNoStr := c.Query("pageno", "1")
pageSizeStr := c.Query("pagesize", "10")
pageNo, _ := strconv.Atoi(pageNoStr)
pageSize, _ := strconv.Atoi(pageSizeStr)
requests, total, err := ctl.customerService.GetCustomerRequests(customerIDStr, pageNo, pageSize)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": 200,
"message": "Customer requests fetched successfully",
"status": true,
"data": requests,
"total": total,
})
}
func (ctl *CustomerController) GetTenantCustomers(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
keyword := c.Query("keyword")
result := ctl.customerService.GetTenantCustomers(tid, lid, pageno, pagesize, keyword)
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *CustomerController) SearchCustomer(c *fiber.Ctx) error {
keyword := c.Query("keyword")
tid, _ := strconv.Atoi(c.Query("tenantid"))
result := ctl.customerService.SearchCustomer(keyword, tid)
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *CustomerController) CreateCustomer(c *fiber.Ctx) error {
var data models.Customers
if err := c.BodyParser(&data); err != nil {
return err
}
result, err := ctl.customerService.CreateCustomer(data)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusCreated,
"message": "Successful",
"details": result,
})
}
func (ctl *CustomerController) CustomerLogin(c *fiber.Ctx) error {
var body struct {
Contactno string `json:"contactno"`
}
if err := c.BodyParser(&body); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusBadRequest,
"status": false,
"message": "Invalid request",
"error": err.Error(),
})
}
customer, err := ctl.customerService.LoginCustomer(body.Contactno)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"message": "Failed to login",
"error": err.Error(),
})
}
if customer == nil {
return c.JSON(fiber.Map{
"code": http.StatusConflict,
"status": false,
"message": "Account not found",
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"status": true,
"message": "Success",
"details": customer,
})
}

View File

@@ -0,0 +1,261 @@
package controllers
import (
"net/http"
"strconv"
"nearle/models"
"nearle/services"
"github.com/gofiber/fiber/v2"
)
type DeliveriesController struct {
deliveriesService services.DeliveriesService
}
func NewDeliveriesController(deliveriesService services.DeliveriesService) *DeliveriesController {
return &DeliveriesController{deliveriesService: deliveriesService}
}
func (ctl *DeliveriesController) CreateDeliveries(c *fiber.Ctx) error {
var deliveries []models.Deliveries
if err := c.BodyParser(&deliveries); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"status": false,
"message": "Invalid request body",
})
}
err := ctl.deliveriesService.CreateDeliveriesService(deliveries)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"status": false,
"message": err.Error(),
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": 200,
"status": true,
"message": "Deliveries created successfully",
})
}
func (ctl *DeliveriesController) UpdateDelivery(c *fiber.Ctx) error {
var data models.UpdateDeliveryStatus
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
err := ctl.deliveriesService.UpdateDeliveryService(data)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"code": http.StatusCreated,
"message": "Success",
"status": true,
})
}
func (ctl *DeliveriesController) GetDeliverySummary(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
uid, _ := strconv.Atoi(c.Query("userid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
fdate := c.Query("fromdate")
tdate := c.Query("todate")
data, err := ctl.deliveriesService.GetDeliverySummary(tid, pid, uid, aid, lid, fdate, tdate)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *DeliveriesController) GetDeliveryInsight(c *fiber.Ctx) error {
// Get tenantid from query param
tenantIDStr := c.Query("tenantid")
tenantID, _ := strconv.Atoi(tenantIDStr)
// Call service with tenantID
locations, err := ctl.deliveriesService.GetDeliveryInsightService(tenantID)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": locations,
})
}
func (ctl *DeliveriesController) GetLocationDeliverySummary(c *fiber.Ctx) error {
tenantIDStr := c.Query("tenantid")
tenantID, _ := strconv.Atoi(tenantIDStr)
data, err := ctl.deliveriesService.GetLocationDeliverySummary(tenantID)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *DeliveriesController) GetReportSummary(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
uid, _ := strconv.Atoi(c.Query("userid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
fdate := c.Query("fromdate")
tdate := c.Query("todate")
data, err := ctl.deliveriesService.GetReportSummary(tid, pid, uid, aid, fdate, tdate)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *DeliveriesController) GetRiderSummary(c *fiber.Ctx) error {
aid, _ := strconv.Atoi(c.Query("applocationid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
tid, _ := strconv.Atoi(c.Query("tenantid"))
fdate := c.Query("fromdate")
tdate := c.Query("todate")
data, err := ctl.deliveriesService.GetRiderSummary(aid, pid, tid, fdate, tdate)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *DeliveriesController) GetDeliveries(c *fiber.Ctx) error {
pid, _ := strconv.Atoi(c.Query("partnerid"))
tid, _ := strconv.Atoi(c.Query("tenantid"))
uid, _ := strconv.Atoi(c.Query("userid"))
cid, _ := strconv.Atoi(c.Query("customerid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
aud, _ := strconv.Atoi(c.Query("appuserid"))
lid, _ := strconv.Atoi(c.Query("locationid")) // 👈 NEW
pageno, _ := strconv.Atoi(c.Query("pageno", "1"))
pagesize, _ := strconv.Atoi(c.Query("pagesize", ""))
fdate := c.Query("fromdate")
tdate := c.Query("todate")
stat := c.Query("status")
keyword := c.Query("keyword")
info := models.DeliveryQuery{
Partnerid: pid,
Tenantid: tid,
UserID: uid,
Customerid: cid,
Appuserid: aud,
Applocationid: aid,
Locationid: lid, // 👈 NEW
Fromdate: fdate,
ToDate: tdate,
Status: stat,
Pageno: pageno,
Pagesize: pagesize,
Keyword: keyword,
}
result := ctl.deliveriesService.GetDeliveries(info)
if result == nil {
result = []models.Deliveryinfo{}
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *DeliveriesController) GetDeliveryQueues(c *fiber.Ctx) error {
fdate := c.Query("fromdate")
tdate := c.Query("todate")
uid, _ := strconv.Atoi(c.Query("userid"))
result, err := ctl.deliveriesService.GetDeliveryQueues(uid, fdate, tdate)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}

View File

@@ -0,0 +1,356 @@
package controllers
import (
"log"
"nearle/models"
"net/http"
"strconv"
"strings"
"time"
"nearle/services"
"github.com/gofiber/fiber/v2"
)
type OrderController struct {
orderService services.OrderService
}
func NewOrderController(orderService services.OrderService) *OrderController {
return &OrderController{orderService: orderService}
}
func (ctl *OrderController) GetOrders(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
cid, _ := strconv.Atoi(c.Query("customerid"))
mid, _ := strconv.Atoi(c.Query("moduleid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
uid, _ := strconv.Atoi(c.Query("appuserid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
configid, _ := strconv.Atoi(c.Query("configid"))
stat := c.Query("status")
fdate := c.Query("fromdate")
tdate := c.Query("todate")
keyword := c.Query("keyword")
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
if pageno <= 0 {
pageno = 1
}
if pagesize <= 0 {
pagesize = 10
}
// Build dynamic query struct
query := models.DeliveryQuery{
Tenantid: tid,
Partnerid: pid,
Customerid: cid,
Moduleid: mid,
Applocationid: aid,
Locationid: lid,
UserID: uid,
Appuserid: uid,
Configid: configid,
Fromdate: fdate,
ToDate: tdate,
Status: stat,
Keyword: keyword,
Pageno: pageno,
Pagesize: pagesize,
}
var (
orders []models.OrderInfo
err error
)
// --------------------------
// 🔥 DYNAMIC ROUTING LOGIC
// --------------------------
if tid != 0 && lid != 0 {
// ⭐ Both tenant & location → special handler
orders, err = ctl.orderService.GetTenantLocationOrders(query)
} else if tid != 0 {
// Tenant only
orders, err = ctl.orderService.GetTenantOrders(query)
} else if pid != 0 {
// Partner
orders, err = ctl.orderService.GetPartnerOrders(stat, fdate, tdate, pid, pageno, pagesize, keyword)
} else if cid != 0 {
// Customer
orders, err = ctl.orderService.GetCustomerOrders(stat, fdate, tdate, cid, mid, pageno, pagesize, keyword)
} else if aid != 0 {
// App-location orders
orders, err = ctl.orderService.GetAdminOrders(stat, fdate, tdate, aid, pageno, pagesize, keyword)
} else if uid != 0 {
// User orders
orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword)
} else {
// All orders
orders, err = ctl.orderService.GetAllOrders(stat, fdate, tdate, pageno, pagesize, keyword)
}
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"details": orders,
})
}
func (ctl *OrderController) GetOrderSummary(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
pid, _ := strconv.Atoi(c.Query("partnerid"))
cid, _ := strconv.Atoi(c.Query("customerid"))
lid, _ := strconv.Atoi(c.Query("locationid"))
fdate := c.Query("fromdate")
tdate := c.Query("todate")
data, err := ctl.orderService.GetOrderSummary(tid, pid, cid, lid, fdate, tdate)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *OrderController) GetlocationOrderSummary(c *fiber.Ctx) error {
tenantIDStr := c.Query("tenantid")
tenantID, _ := strconv.Atoi(tenantIDStr)
data, err := ctl.orderService.GetLocationOrderSummary(tenantID)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *OrderController) GetOrderInsights(c *fiber.Ctx) error {
tenantIDStr := c.Query("tenantid")
tenantID, _ := strconv.Atoi(tenantIDStr)
insights, err := ctl.orderService.GetOrderInsights(tenantID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"message": "Failed to fetch order insights",
"error": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"status": true,
"message": "Success",
"details": insights,
})
}
func (ctl *OrderController) GetOrderDetails(c *fiber.Ctx) error {
orderHeaderIDStr := c.Query("orderheaderid")
if orderHeaderIDStr == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"message": "orderheaderid is required",
"status": false,
"details": []interface{}{},
})
}
orderHeaderID, err := strconv.Atoi(orderHeaderIDStr)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"message": "invalid orderheaderid",
"status": false,
"details": []interface{}{},
})
}
details, err := ctl.orderService.GetOrderDetails(orderHeaderID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch order details",
"status": false,
"details": []interface{}{},
})
}
var orderAmount, totalTaxAmount float64
if len(details) > 0 {
orderAmount = details[0].Orderamount
totalTaxAmount = details[0].Totaltaxamount
}
return c.JSON(fiber.Map{
"code": 200,
"pricedetails": fiber.Map{
"orderamount": orderAmount,
"totaltaxamount": totalTaxAmount,
},
"details": details,
"message": "Success",
"status": true,
})
}
func (ctl *OrderController) UpdateOrder(c *fiber.Ctx) error {
var order models.Orders
if err := c.BodyParser(&order); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.orderService.UpdateOrder(&order); err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": "Error updating order",
"status": false,
})
}
return c.Status(http.StatusAccepted).JSON(fiber.Map{
"code": http.StatusAccepted,
"message": "Success",
"status": true,
})
}
func (ctl *OrderController) CreateOrderv3(c *fiber.Ctx) error {
var data models.Orders
// 🛠️ Strategy 1: Try parsing direct object (for Apidog)
if err := c.BodyParser(&data); err != nil {
log.Println("BodyParser strategy 1 error:", err)
}
// 🛠️ Strategy 2: If Strategy 1 didn't find a Tenantid, try parsing wrapped object (for Mobile App)
if data.Tenantid == 0 {
type OrderWrapper struct {
Orders models.Orders `json:"orders"`
}
var wrapper OrderWrapper
if err := c.BodyParser(&wrapper); err == nil && wrapper.Orders.Tenantid != 0 {
data = wrapper.Orders
}
}
// Double check we have the required ID
if data.Tenantid == 0 {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": "Tenant ID is required",
"status": false,
})
}
if strings.TrimSpace(data.Orderdate) == "" {
data.Orderdate = time.Now().Format("2006-01-02 15:04:05")
}
if strings.TrimSpace(data.Deliverytime) == "" {
data.Deliverytime = time.Now().Format("2006-01-02 15:04:05")
}
order, err := ctl.orderService.CreateOrder(data)
if err != nil {
log.Println("CreateOrder service error:", err)
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Order created successfully",
"status": true,
"details": order,
})
}
func (ctl *OrderController) GetCustomerOrders(c *fiber.Ctx) error {
customerID := c.Query("customerid")
tenantID := c.Query("tenantid")
moduleID := c.Query("moduleid")
fromDate := c.Query("fromdate")
toDate := c.Query("todate")
orderStatus := c.Query("orderstatus")
keyword := c.Query("keyword")
pageNo, _ := strconv.Atoi(c.Query("pageno", "1"))
pageSize, _ := strconv.Atoi(c.Query("pagesize", "10"))
if pageNo < 1 {
pageNo = 1
}
if pageSize < 1 {
pageSize = 10
}
offset := (pageNo - 1) * pageSize
orders, err := ctl.orderService.GetCustomerOrdersv3(customerID, tenantID, moduleID, fromDate, toDate, orderStatus, keyword, pageSize, offset)
if err != nil {
log.Println("GetCustomerOrders error:", err)
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"status": false,
"message": "Failed to fetch customer orders",
"error": err.Error(),
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"status": true,
"message": "Customer orders fetched successfully",
"data": orders,
})
}

View File

@@ -0,0 +1,156 @@
package controllers
import (
"nearle/services"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
)
type PartnerController struct {
partnerService services.PartnerService
}
func NewPartnerController(partnerService services.PartnerService) *PartnerController {
return &PartnerController{partnerService: partnerService}
}
func (ctl *PartnerController) GetActiveRiders(c *fiber.Ctx) error {
pid, _ := strconv.Atoi(c.Query("partnerid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
uid, _ := strconv.Atoi(c.Query("userid"))
tid,_ := strconv.Atoi(c.Query("tenantid"))
result, err := ctl.partnerService.GetActiveRiders(pid, aid, uid, tid)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
func (ctl *PartnerController) GetPartners(c *fiber.Ctx) error {
pid, _ := strconv.Atoi(c.Query("partnerid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
uid, _ := strconv.Atoi(c.Query("userid"))
result, err := ctl.partnerService.GetPartners(aid, pid, uid)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
func (ctl *PartnerController) GetRiderShifts(c *fiber.Ctx) error {
aid, _ := strconv.Atoi(c.Query("applocationid"))
result, err := ctl.partnerService.GetRiderShifts(aid)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
func (ctl *PartnerController) GetLocationConfig(c *fiber.Ctx) error {
uid, _ := strconv.Atoi(c.Query("userid"))
cid, _ := strconv.Atoi(c.Query("configid"))
result, err := ctl.partnerService.GetLocationConfig(uid, cid)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
func (ctl *PartnerController) GetRiderLogs(c *fiber.Ctx) error {
pid, _ := strconv.Atoi(c.Query("partnerid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
fdate := c.Query("fromdate")
tdate := c.Query("fromdate")
data, err := ctl.partnerService.GetRiderLogs(pid, aid, fdate, tdate)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": data,
})
}
func (ctl *PartnerController) GetRiderInfo(c *fiber.Ctx) error {
uid, _ := strconv.Atoi(c.Query("userid"))
result, err := ctl.partnerService.GetRiderInfo(uid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}

View File

@@ -0,0 +1,516 @@
package controllers
import (
"net/http"
"strconv"
"nearle/models"
"nearle/services"
"github.com/gofiber/fiber/v2"
)
type ProductController struct {
productService services.ProductService
}
func NewProductController(productService services.ProductService) *ProductController {
return &ProductController{productService: productService}
}
func (ctl *ProductController) GetProductSubCategory(c *fiber.Ctx) error {
categoryID, _ := strconv.Atoi(c.Query("categoryid", "0"))
tenantID, _ := strconv.Atoi(c.Query("tenantid", "0"))
data, err := ctl.productService.GetProductSubCategory(categoryID, tenantID)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": 200,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetProductCount(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
categoryID, _ := strconv.Atoi(c.Query("categoryid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
// locationID, _ := strconv.Atoi(c.Query("locationid"))
approve := c.Query("approve", "")
data, err := ctl.productService.GetProductCount(tenantID, categoryID, subcategoryID, approve)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusConflict,
"message": "failed",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetProductCategory(c *fiber.Ctx) error {
data, err := ctl.productService.GetProductCategory()
if err != nil {
return c.JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch categories",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetProductVariants(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
data, err := ctl.productService.GetProductVariants(tenantID, subcategoryID)
if err != nil {
return c.JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch Product varients",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetCatalougeProducts(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
keyword := c.Query("keyword")
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
data, err := ctl.productService.GetCatalougeProducts(tenantID, locationID, subcategoryID, pageno, pagesize, keyword)
if err != nil {
return c.JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch Catalouge Product",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetProductStocks(c *fiber.Ctx) error {
tenantID := c.Query("tenantid")
locationID := c.Query("locationid")
stocks, err := ctl.productService.GetProductStocks(tenantID, locationID)
if err != nil {
return c.JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch product stocks",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": 200,
"message": "Product stocks fetched successfully",
"status": true,
"data": stocks,
})
}
func (ctl *ProductController) CreateProductStock(c *fiber.Ctx) error {
var stocks []models.Productstock
if err := c.BodyParser(&stocks); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.productService.CreateProductStock(stocks); err != nil {
return c.JSON(fiber.Map{
"code": 500,
"message": "Failed to create product stocks",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": 201,
"message": "Product stocks created successfully",
"status": true,
"details": stocks,
})
}
func (ctl *ProductController) CreateProduct(c *fiber.Ctx) error {
var product models.Products
if err := c.BodyParser(&product); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.productService.CreateProduct(product); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": "Failed to create product",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusCreated,
"message": "Product created successfully",
"status": true,
"data": product,
})
}
func (ctl *ProductController) UpdateProduct(c *fiber.Ctx) error {
var product models.Products
if err := c.BodyParser(&product); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.productService.UpdateProduct(product); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusAccepted,
"message": "Product updated successfully",
"status": true,
"data": product,
})
}
func (ctl *ProductController) DeleteProduct(c *fiber.Ctx) error {
pidStr := c.Query("productid")
pid, err := strconv.Atoi(pidStr)
if err != nil || pid <= 0 {
return c.JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid product ID",
"status": false,
})
}
if err := ctl.productService.DeleteProduct(pid); err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Product deleted successfully",
"status": true,
})
}
func (ctl *ProductController) GetStockStatement(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
keyword := c.Query("keyword")
data, err := ctl.productService.GetStockStatement(tenantID, locationID, subcategoryID, pageno, pagesize, keyword)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *ProductController) GetLocationProducts(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
keyword := c.Query("keyword")
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
result, err := ctl.productService.GetLocationProducts(tenantID, locationID, subcategoryID, pageno, pagesize, keyword)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"details": result,
})
}
func (ctl *ProductController) GetLocationProductSummary(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
result, err := ctl.productService.GetLocationProductSummary(tenantID, locationID)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusInternalServerError,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"details": result,
})
}
func (ctl *ProductController) GetAllProducts(c *fiber.Ctx) error {
categoryID, _ := strconv.Atoi(c.Query("categoryid"))
subcategoryID, _ := strconv.Atoi(c.Query("subcategoryid"))
productID, _ := strconv.Atoi(c.Query("productid"))
applocationID, _ := strconv.Atoi(c.Query("applocationid"))
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
locationID, _ := strconv.Atoi(c.Query("locationid"))
keyword := c.Query("keyword", "")
productStatus := c.Query("productstatus", "")
approve := c.Query("approve", "")
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
details, err := ctl.productService.FetchFilteredProducts(
categoryID, subcategoryID, productID, applocationID, tenantID,
locationID, keyword, productStatus, approve, pageno, pagesize,
)
if err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Success",
"data": details,
})
}
func (ctl *ProductController) GetProductByVariant(c *fiber.Ctx) error {
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
variantid, _ := strconv.Atoi(c.Query("variantid"))
result, err := ctl.productService.GetProductByVariant(tenantID, variantid)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusConflict,
"message": "failed",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *ProductController) GetProductsBySubcategory(c *fiber.Ctx) error {
categoryID, err := strconv.Atoi(c.Query("categoryid"))
if err != nil || categoryID == 0 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"status": false,
"message": "Valid categoryid is required",
"data": fiber.Map{},
})
}
params := models.ProductFilter{
CategoryID: categoryID,
TenantID: parseInt(c.Query("tenantid")),
AppLocationID: parseInt(c.Query("applocationid")),
ProductID: parseInt(c.Query("productid")),
Keyword: c.Query("keyword"),
LocationID: parseInt(c.Query("locationid")),
}
result, err := ctl.productService.GetProductsBySubcategory(params)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"status": false,
"message": err.Error(),
"data": fiber.Map{},
})
}
return c.JSON(fiber.Map{
"code": 200,
"status": true,
"message": "Success",
"data": result,
})
}
func parseInt(s string) int {
if s == "" {
return 0
}
val, _ := strconv.Atoi(s)
return val
}
func (ctl *ProductController) UpdateProductLocation(c *fiber.Ctx) error {
var data models.Productlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
if err := ctl.productService.UpdateProductLocation(data); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.Status(http.StatusAccepted).JSON(fiber.Map{
"status": true,
"code": http.StatusAccepted,
"message": "Product update successful",
})
}
func (ctl *ProductController) CreateProductLocation(c *fiber.Ctx) error {
var data []models.Productlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.productService.CreateProductLocation(data); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"status": true,
"code": http.StatusCreated,
"message": "Success",
})
}
func (ctl *ProductController) CreateProductVariant(c *fiber.Ctx) error {
var input models.Productvariant
// Parse request body
if err := c.BodyParser(&input); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
// Call service layer
if err := ctl.productService.CreateProductVariant(input); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
})
}

View File

@@ -0,0 +1,436 @@
package controllers
import (
"fmt"
"log"
"nearle/models"
"nearle/services"
"net/http"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
)
type TenantController struct {
tenantService services.TenantService
}
func NewTenantController(tenantService services.TenantService) *TenantController {
return &TenantController{tenantService: tenantService}
}
func (ctl *TenantController) SearchTenant(c *fiber.Ctx) error {
status := c.Query("status")
searchStr := c.Query("keyword")
data, err := ctl.tenantService.SearchTenant(status, searchStr)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("Error searching tenants: %v", err),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) GetAllTenants(c *fiber.Ctx) error {
pageno, _ := strconv.Atoi(c.Query("pageno"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
status := c.Query("status")
aid, _ := strconv.Atoi(c.Query("applocationid"))
tenanttype := c.Query("tenanttype")
keyword := c.Query("keyword")
details, err := ctl.tenantService.GetAllTenants(pageno, pagesize, aid, status, tenanttype, keyword)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("Error getting all tenants: %v", err),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": details,
})
}
func (ctl *TenantController) GetTenantLocations(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
data, err := ctl.tenantService.GetTenantLocations(tid)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("Error getting tenant locations: %v", err),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) GetTenantSlot(c *fiber.Ctx) error {
data, err := ctl.tenantService.GetTenantSlot()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("Error getting tenant slots: %v", err),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) CreateTenantCustomer(c *fiber.Ctx) error {
var req models.CreateTenantCustomerRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"status": false,
"message": "Invalid request body",
"data": fiber.Map{},
})
}
tenantCustomer, err := ctl.tenantService.CreateTenantCustomer(req)
if err != nil {
if strings.Contains(err.Error(), "already exists for this location") {
return c.JSON(fiber.Map{
"code": 409,
"status": false,
"message": "Customer already assigned to this location",
"data": fiber.Map{},
})
}
log.Println("Error inserting tenant customer:", err)
return c.JSON(fiber.Map{
"code": 500,
"status": false,
"message": "Failed to create tenant customer",
"data": fiber.Map{},
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": 200,
"status": true,
"message": "Tenant customer created successfully",
"data": tenantCustomer,
})
}
func (ctl *TenantController) GetCustomerTenants(c *fiber.Ctx) error {
customerID, err := strconv.Atoi(c.Query("customerid"))
if err != nil || customerID == 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"message": "Invalid customerid",
"status": false,
"details": []interface{}{},
})
}
categoryID, _ := strconv.Atoi(c.Query("categoryid"))
tenantFlag, _ := strconv.Atoi(c.Query("tenant")) // 0 = all tenants, 1 = tenants with orders
data, err := ctl.tenantService.GetCustomerTenants(customerID, categoryID, tenantFlag)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": err.Error(),
"status": false,
"details": []interface{}{},
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data.Details,
})
}
func (ctl *TenantController) GetTenantPricing(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
aid, _ := strconv.Atoi(c.Query("applocationid"))
data, err := ctl.tenantService.GetTenantPricing(tid, aid)
if err != nil {
return c.JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) UpdateLocation(c *fiber.Ctx) error {
var data models.Tenantlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
if err := ctl.tenantService.UpdateLocation(data); err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusAccepted,
"message": "Location update successful",
})
}
func (ctl *TenantController) CreateLocation(c *fiber.Ctx) error {
var data models.Tenantlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
err := ctl.tenantService.CreateLocation(data)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"code": http.StatusCreated,
"message": "Location Successfully Created",
"status": true,
})
}
func (ctl *TenantController) GetStaffs(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(c.Query("tenantid"))
data, err := ctl.tenantService.GetStaffs(tid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) CreateStaff(c *fiber.Ctx) error {
var data models.User
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
if err := ctl.tenantService.CreateStaff(data); err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusConflict,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusCreated,
"message": "Staff created successfully",
"status": true,
})
}
func (ctl *TenantController) UpdateStaff(c *fiber.Ctx) error {
var data models.User
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
if err := ctl.tenantService.UpdateStaff(data); err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusAccepted,
"message": "Staff updated successfully",
})
}
func (ctl *TenantController) CreateTenantLocation(c *fiber.Ctx) error {
var data models.Tenantlocations
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"code": 400,
"message": "Invalid request body",
"status": false,
})
}
resp := ctl.tenantService.CreateTenantLocation(data)
return c.Status(fiber.StatusOK).JSON(resp)
}
func (ctl *TenantController) UpdateTenantLocation(c *fiber.Ctx) error {
var data models.Tenantlocations
// Parse JSON body
if err := c.BodyParser(&data); err != nil {
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"status": false,
"code": 400,
"message": "Invalid request body",
})
}
// Call service layer
resp := ctl.tenantService.UpdateTenantLocation(data)
// Always return HTTP 200 (as per your API pattern)
return c.Status(fiber.StatusOK).JSON(resp)
}
func (ctl *TenantController) CreateTenantUser(c *fiber.Ctx) error {
var data models.Tenants
if err := c.BodyParser(&data); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"status": false,
"message": "Invalid request body",
})
}
result, err := ctl.tenantService.CreateTenantUser(data)
if err != nil {
if err.Error() == "Tenant Already Exists" {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": 409,
"status": false,
"message": err.Error(),
})
}
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"status": false,
"message": err.Error(),
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"code": 201,
"status": true,
"message": "Successfully Created",
"details": result,
})
}
func (ctl *TenantController) GetTenantInfo(c *fiber.Ctx) error {
tid, _ := strconv.Atoi(strings.TrimSpace(c.Query("tenantid")))
locationid, _ := strconv.Atoi(strings.TrimSpace(c.Query("locationid")))
data, err := ctl.tenantService.GetTenantByID(tid, locationid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": "Error fetching tenant info",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}
func (ctl *TenantController) GetTenantByKeyword(c *fiber.Ctx) error {
keyword := c.Query("keyword")
data, err := ctl.tenantService.GetTenantByKeyword(keyword)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": "Error searching tenants by keyword",
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": data,
})
}

View File

@@ -0,0 +1,230 @@
package controllers
import (
"net/http"
"strconv"
"nearle/models"
"nearle/services"
"github.com/gofiber/fiber/v2"
)
type UserController struct {
userService services.UserService
}
func NewUserController(userService services.UserService) *UserController {
return &UserController{userService: userService}
}
func (ctl *UserController) GetAllUsers(c *fiber.Ctx) error {
roleID, _ := strconv.Atoi(c.Query("roleid", "0"))
tenantID, _ := strconv.Atoi(c.Query("tenantid", "0"))
pageno, _ := strconv.Atoi(c.Query("pageno", "1"))
pagesize, _ := strconv.Atoi(c.Query("pagesize", "10"))
keyword := c.Query("keyword", "")
users, err := ctl.userService.GetAllUsers(roleID, tenantID, pageno, pagesize, keyword)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": users,
})
}
func (ctl *UserController) GetUserInfo(c *fiber.Ctx) error {
uid, err := strconv.Atoi(c.Query("userid"))
if err != nil || uid <= 0 {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid userid",
"status": false,
})
}
user, err := ctl.userService.GetUserByID(uid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": user,
})
}
func (ctl *UserController) Login(c *fiber.Ctx) error {
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid request body",
"status": false,
})
}
info, err := ctl.userService.Login(user)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": "User not found",
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": info,
})
}
func (ctl *UserController) TenantLogin(c *fiber.Ctx) error {
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
info, err := ctl.userService.TenantLogin(user)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": info,
})
}
func (ctl *UserController) UpdateStaff(c *fiber.Ctx) error {
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": http.StatusBadRequest,
"message": "Invalid request body",
})
}
if err := ctl.userService.UpdateStaff(user); err != nil {
return c.JSON(fiber.Map{
"status": false,
"code": http.StatusConflict,
"message": err.Error(),
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusAccepted,
"message": "User update successful",
})
}
func (ctl *UserController) AppLogin(c *fiber.Ctx) error {
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": 400,
"status": false,
"message": "Invalid request body",
})
}
_, resp, err := ctl.userService.AppLogin(user)
if err != nil {
// Use resp.Code if present, fallback to 409
code := http.StatusConflict
if v, ok := resp["code"].(int); ok {
code = v
}
return c.Status(code).JSON(resp)
}
// ✅ Always return resp
return c.Status(http.StatusOK).JSON(resp)
}
func (ctl *UserController) CreateUser(c *fiber.Ctx) error {
var user models.User
// Parse request body
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"status": false,
"message": "Invalid request body",
})
}
// Call service
info, err := ctl.userService.CreateUser(user)
if err != nil {
return c.Status(http.StatusConflict).JSON(fiber.Map{
"code": http.StatusConflict,
"status": false,
"message": "Failed",
})
}
return c.Status(http.StatusCreated).JSON(fiber.Map{
"code": http.StatusCreated,
"status": true,
"message": "Success",
"details": info,
})
}
func (ctl *UserController) TenantWebLogin(c *fiber.Ctx) error {
var user models.User
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"status": false,
"code": fiber.StatusBadRequest,
"message": "Invalid request body",
})
}
info, resp := ctl.userService.TenantWebLogin(user)
// Ensure the response map contains the correct status code
code, ok := resp["code"].(int)
if !ok {
code = fiber.StatusInternalServerError
}
// Include tenant user info if login successful (code 200)
if code == fiber.StatusOK {
resp["details"] = info
}
return c.Status(code).JSON(resp)
}

View File

@@ -0,0 +1,314 @@
package controllers
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"nearle/models"
"nearle/services"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type UtilsController struct {
utilsService services.UtilsService
}
func NewUtilsController(utilsService services.UtilsService) *UtilsController {
return &UtilsController{utilsService: utilsService}
}
const (
fcmURL = "https://fcm.googleapis.com/v1/projects/nearle-gear/messages:send"
scope = "https://www.googleapis.com/auth/firebase.messaging"
serviceAcc = "nearle-gear-firebase-adminsdk-l9oha-23ca3b3609.json" // Path to your service account JSON
)
var client *http.Client
func (ctl *UtilsController) GetAppTypes(c *fiber.Ctx) error {
tag := c.Query("tag")
result, err := ctl.utilsService.GetApptypes(tag)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *UtilsController) NotifyUser(c *fiber.Ctx) error {
var body struct {
Token string `json:"token"`
Notification models.FcmNotification `json:"notification"`
Data map[string]string `json:"data"`
}
if err := c.BodyParser(&body); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": fmt.Sprintf("Error parsing request body: %v", err),
"status": false,
})
}
err := ctl.utilsService.SendNotification(body.Token, body.Notification, body.Data)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("Error sending notification: %v", err),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "FCM message sent successfully!",
"status": true,
})
}
func (ctl *UtilsController) GetSubcategories(c *fiber.Ctx) error {
mid, _ := strconv.Atoi(c.Query("moduleid"))
cid, _ := strconv.Atoi(c.Query("categoryid"))
result, err := ctl.utilsService.GetSubcategories(mid, cid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *UtilsController) GetApplocations(c *fiber.Ctx) error {
aid, err := strconv.Atoi(c.Query("applocationid"))
if err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid applocationid",
"status": false,
})
}
result, err := ctl.utilsService.GetApplocations(aid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
func (ctl *UtilsController) GetApplocationConfig(c *fiber.Ctx) error {
aid, err := strconv.Atoi(c.Query("applocationid"))
if err != nil {
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
"code": http.StatusBadRequest,
"message": "Invalid applocationid",
"status": false,
})
}
result, err := ctl.utilsService.GetApplocationConfig(aid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": http.StatusInternalServerError,
"message": err.Error(),
"status": false,
})
}
return c.JSON(fiber.Map{
"status": true,
"code": http.StatusOK,
"message": "Successful",
"details": result,
})
}
type Notification struct {
Title string `json:"title"`
Body string `json:"body"`
}
type FCMRequestBody struct {
Message struct {
Notification Notification `json:"notification"`
Token string `json:"token"`
} `json:"message"`
}
func GetClientFromServiceAccount() (*http.Client, error) {
data, err := ioutil.ReadFile(serviceAcc)
if err != nil {
return nil, fmt.Errorf("failed to read service account file: %v", err)
}
config, err := google.JWTConfigFromJSON(data, scope)
if err != nil {
return nil, fmt.Errorf("failed to parse service account JSON: %v", err)
}
client := config.Client(oauth2.NoContext)
return client, nil
}
func sendFCMMessage(token, title, body string) error {
if client == nil {
var err error
client, err = GetClientFromServiceAccount()
if err != nil {
return fmt.Errorf("failed to get client: %v", err)
}
}
notification := Notification{
Title: title,
Body: body,
}
fcmRequest := FCMRequestBody{}
fcmRequest.Message.Notification = notification
fcmRequest.Message.Token = token
requestBody, err := json.Marshal(fcmRequest)
if err != nil {
return err
}
req, err := http.NewRequest("POST", fcmURL, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("failed to send message: %s", string(bodyBytes))
}
return nil
}
func (ctl *UtilsController) NotifyAdmin(c *fiber.Ctx) error {
body := new(struct {
Token []string `json:"token"`
Notification struct {
Title string `json:"title"`
Body string `json:"body"`
Sound string `json:"sound"`
Type string `json:"type"`
} `json:"notification"`
})
if err := c.BodyParser(body); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
}
if len(body.Token) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "No tokens provided")
}
var successCount, failureCount int
for _, token := range body.Token {
err := sendFCMMessage(token, body.Notification.Title, body.Notification.Body)
if err != nil {
log.Printf("❌ Failed to send to %s: %v", token, err)
failureCount++
} else {
log.Printf("✅ Notification sent to %s", token)
successCount++
}
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"message": "Admin notifications sent",
"success": successCount,
"failure": failureCount,
})
}
func (ctl *UtilsController) GetAppConfig(c *fiber.Ctx) error {
cid, _ := strconv.Atoi(c.Query("configid"))
result, err := ctl.utilsService.GetAppConfig(cid)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch app config",
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}
func (ctl *UtilsController) GetAppCategory(c *fiber.Ctx) error {
result, err := ctl.utilsService.GetAppCategory()
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
"code": 500,
"message": "Failed to fetch app categories",
"status": false,
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"code": http.StatusOK,
"message": "Success",
"status": true,
"details": result,
})
}