Refactor check earlier registration
[nonrtric/plt/sme.git] / capifcore / internal / publishservice / publishservice.go
index 3898a80..ee3efef 100644 (file)
@@ -158,18 +158,26 @@ func (ps *PublishService) GetApfIdServiceApis(ctx echo.Context, apfId string) er
 // Publish a new API.
 func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) error {
        var newServiceAPIDescription publishapi.ServiceAPIDescription
+       errorMsg := "Unable to publish the service due to %s "
        err := ctx.Bind(&newServiceAPIDescription)
        if err != nil {
-               return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service "+apfId)
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId))
        }
 
+       if ps.isServicePublished(newServiceAPIDescription) {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, "service already published"))
+       }
+
+       if err := newServiceAPIDescription.Validate(); err != nil {
+               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err))
+       }
        ps.lock.Lock()
        defer ps.lock.Unlock()
 
        registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
        for _, profile := range *newServiceAPIDescription.AefProfiles {
                if !slices.Contains(registeredFuncs, profile.AefId) {
-                       return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Function %s not registered", profile.AefId))
+                       return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf(errorMsg, fmt.Sprintf("function %s not registered", profile.AefId)))
                }
        }
 
@@ -200,6 +208,17 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e
        return nil
 }
 
+func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) bool {
+       for _, services := range ps.publishedServices {
+               for _, service := range services {
+                       if service.IsPublished(newService) {
+                               return true
+                       }
+               }
+       }
+       return false
+}
+
 func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) {
        info := strings.Split(*newServiceAPIDescription.Description, ",")
        if len(info) == 5 {
@@ -344,7 +363,6 @@ func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, ev
 }
 
 func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles []publishapi.AefProfile) error {
-
        registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
        for _, profile := range updatedProfiles {
                if !slices.Contains(registeredFuncs, profile.AefId) {
@@ -352,7 +370,6 @@ func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles
                }
        }
        return nil
-
 }
 
 // This function wraps sending of an error in the Error format, and