<commit_before>package service

import (
	"fmt"
	"net/http"
	"time"

	"github.com/dokkur/swanager/api/common"
	"github.com/dokkur/swanager/command"
	"github.com/dokkur/swanager/config"
	"github.com/dokkur/swanager/core/entities"
	"github.com/dokkur/swanager/core/swarm"
	swarm_service "github.com/dokkur/swanager/core/swarm/service"
	"github.com/gin-gonic/gin"
)

// GetRoutesForRouter adds resource routes to api router
func GetRoutesForRouter(router *gin.RouterGroup) {

	services := router.Group("/services", common.Auth(true))
	{
		services.GET("", list)
		services.POST("", create)
	}

	service := services.Group("/:service_id")
	{
		service.GET("", show)
		service.GET("/logs", logs)
		service.DELETE("", delete)
		service.PUT("", update)
		service.PUT("/start", start)
		service.PUT("/stop", stop)

	}
}

func list(c *gin.Context) {
	currentUser := common.MustGetCurrentUser(c)

	withStatuses := c.Query("with_statuses") == "1"
	withVolumeSizes := c.Query("with_volume_sizes") == "1"

	cmd, respChan, errChan := command.NewServiceListCommand(command.ServiceList{
		User:            currentUser,
		ApplicationID:   c.Params.ByName("app_id"),
		WithStatuses:    withStatuses,
		WithVolumeSizes: withVolumeSizes,
	})
	command.RunAsync(cmd)

	select {
	case services := <-respChan:
		c.JSON(http.StatusOK, gin.H{"services": services})
	case err := <-errChan:
		common.RenderError(c, http.StatusInternalServerError, err.Error())
	case <-time.After(time.Second * time.Duration(config.RequestTimeout)):
		common.RenderError(c, http.StatusRequestTimeout, "Timeout")
	}
}

func delete(c *gin.Context) {
	cmd, respChan, errChan := command.NewServiceDeleteCommand(command.ServiceDelete{
		User:      common.MustGetCurrentUser(c),
		ServiceID: c.Param("service_id"),
	})
	command.RunAsync(cmd)

	select {
	case service := <-respChan:
		c.JSON(http.StatusOK, gin.H{"service": service})
	case err := <-errChan:<commit_msg>Services app_id filter fixed<commit_after>package service

import (
	"fmt"
	"net/http"
	"time"

	"github.com/dokkur/swanager/api/common"
	"github.com/dokkur/swanager/command"
	"github.com/dokkur/swanager/config"
	"github.com/dokkur/swanager/core/entities"
	"github.com/dokkur/swanager/core/swarm"
	swarm_service "github.com/dokkur/swanager/core/swarm/service"
	"github.com/gin-gonic/gin"
)

// GetRoutesForRouter adds resource routes to api router
func GetRoutesForRouter(router *gin.RouterGroup) {

	services := router.Group("/services", common.Auth(true))
	{
		services.GET("", list)
		services.POST("", create)
	}

	service := services.Group("/:service_id")
	{
		service.GET("", show)
		service.GET("/logs", logs)
		service.DELETE("", delete)
		service.PUT("", update)
		service.PUT("/start", start)
		service.PUT("/stop", stop)

	}
}

func list(c *gin.Context) {
	currentUser := common.MustGetCurrentUser(c)

	withStatuses := c.Query("with_statuses") == "1"
	withVolumeSizes := c.Query("with_volume_sizes") == "1"

	cmd, respChan, errChan := command.NewServiceListCommand(command.ServiceList{
		User:            currentUser,
		ApplicationID:   c.Query("app_id"),
		WithStatuses:    withStatuses,
		WithVolumeSizes: withVolumeSizes,
	})
	command.RunAsync(cmd)

	select {
	case services := <-respChan:
		c.JSON(http.StatusOK, gin.H{"services": services})
	case err := <-errChan:
		common.RenderError(c, http.StatusInternalServerError, err.Error())
	case <-time.After(time.Second * time.Duration(config.RequestTimeout)):
		common.RenderError(c, http.StatusRequestTimeout, "Timeout")
	}
}

func delete(c *gin.Context) {
	cmd, respChan, errChan := command.NewServiceDeleteCommand(command.ServiceDelete{
		User:      common.MustGetCurrentUser(c),
		ServiceID: c.Param("service_id"),
	})
	command.RunAsync(cmd)

	select {
	case service := <-respChan:
		c.JSON(http.StatusOK, gin.H{"service": service})
	case err := <-errChan:
{'fraction_non_alphanumeric': 0.1597602293458431, 'fraction_numerical': 0.0010424811050299713, 'mean_word_length': 4.788838612368024, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5733910', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1366, 'n_words': 222}