Exemple #1
0
        public ClustersModule(IClusterService service, IUserService userService) : base("clusters")
        {
            this.RequiresAuthentication();

            Get("/", x =>
            {
                Debug.Log($"Listing cluster");
                try
                {
                    string filter = Request.Query["filter"];
                    int offset    = Request.Query["offset"];
                    // TODO: Items per page should be read from personal user settings.
                    //       This value should be independent for each module: maps, vehicles and simulation.
                    //       But for now 5 is just an arbitrary value to ensure that we don't try and Page a count of 0
                    int count = Request.Query["count"] > 0 ? Request.Query["count"] : Config.DefaultPageSize;
                    return(service.List(filter, offset, count, this.Context.CurrentUser.Identity.Name).Select(ClusterResponse.Create).ToArray());
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return(Response.AsJson(new { error = $"Failed to list clusters: {ex.Message}" }, HttpStatusCode.InternalServerError));
                }
            });

            Get("/{id:long}", x =>
            {
                long id = x.id;
                Debug.Log($"Getting cluster with id {id}");
                try
                {
                    var cluster = service.Get(id, this.Context.CurrentUser.Identity.Name);
                    return(ClusterResponse.Create(cluster));
                }
                catch (IndexOutOfRangeException)
                {
                    Debug.Log($"Cluster with id {id} does not exist");
                    return(Response.AsJson(new { error = $"Cluster with id {id} does not exist" }, HttpStatusCode.NotFound));
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return(Response.AsJson(new { error = $"Failed to get cluster with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError));
                }
            });

            Post("/", x =>
            {
                Debug.Log($"Adding new cluster");
                try
                {
                    var req = this.BindAndValidate <ClusterRequest>();
                    if (!ModelValidationResult.IsValid)
                    {
                        var message = ModelValidationResult.Errors.First().Value.First().ErrorMessage;
                        Debug.Log($"Validation for adding cluster failed: {message}");
                        return(Response.AsJson(new { error = $"Failed to add cluster: {message}" }, HttpStatusCode.BadRequest));
                    }

                    var cluster = req.ToModel(this.Context.CurrentUser.Identity.Name);

                    cluster.Status = "Valid";

                    long id = service.Add(cluster);
                    Debug.Log($"Cluster added with id {id}");
                    cluster.Id = id;
                    SIM.LogWeb(SIM.Web.ClusterAddName, cluster.Name);
                    SIM.LogWeb(SIM.Web.ClusterAddIPS, cluster.Ips);

                    return(ClusterResponse.Create(cluster));
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return(Response.AsJson(new { error = $"Failed to add cluster: {ex.Message}" }, HttpStatusCode.InternalServerError));
                }
            });

            Put("/{id:long}", x =>
            {
                long id = x.id;
                Debug.Log($"Updating cluster with id {id}");

                try
                {
                    if (id == 0)
                    {
                        throw new Exception("Cannot edit default cluster");
                    }

                    var req = this.BindAndValidate <ClusterRequest>();
                    if (!ModelValidationResult.IsValid)
                    {
                        var message = ModelValidationResult.Errors.First().Value.First().ErrorMessage;
                        Debug.Log($"Validation for updating cluster failed: {message}");
                        return(Response.AsJson(new { error = $"Failed to update cluster: {message}" }, HttpStatusCode.BadRequest));
                    }

                    var cluster = service.Get(id, this.Context.CurrentUser.Identity.Name);

                    cluster.Name = req.name;
                    cluster.Ips  = string.Join(",", req.ips);

                    int result = service.Update(cluster);
                    SIM.LogWeb(SIM.Web.ClusterEditName, cluster.Name);
                    SIM.LogWeb(SIM.Web.ClusterEditIPS, cluster.Ips);
                    if (result > 1)
                    {
                        throw new Exception($"More than one cluster has id {id}");
                    }
                    else if (result < 1)
                    {
                        throw new IndexOutOfRangeException();
                    }

                    cluster.Status = "Valid";

                    return(ClusterResponse.Create(cluster));
                }
                catch (IndexOutOfRangeException)
                {
                    Debug.Log($"Cluster with id {id} does not exist");
                    return(Response.AsJson(new { error = $"Cluster with id {id} does not exist" }, HttpStatusCode.NotFound));
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return(Response.AsJson(new { error = $"Failed to update cluster with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError));
                }
            });

            Delete("/{id:long}", x =>
            {
                long id = x.id;
                Debug.Log($"Removing cluster with id {id}");
                try
                {
                    if (id == 0)
                    {
                        throw new Exception("Cannot remove default cluster");
                    }

                    try
                    {
                        var clusterModel = service.Get(id, this.Context.CurrentUser.Identity.Name);
                        SIM.LogWeb(SIM.Web.ClusterDeleteName, clusterModel.Name);
                        SIM.LogWeb(SIM.Web.ClusterDeleteIPS, clusterModel.Ips);
                    }
                    catch
                    { };
                    int result = service.Delete(id, this.Context.CurrentUser.Identity.Name);
                    if (result > 1)
                    {
                        throw new Exception($"More than one cluster has id {id}");
                    }

                    if (result < 1)
                    {
                        throw new IndexOutOfRangeException();
                    }

                    return(new { });
                }
                catch (IndexOutOfRangeException)
                {
                    Debug.Log($"Cluster with id {id} does not exist");
                    return(Response.AsJson(new { error = $"Cluster with id {id} does not exist" }, HttpStatusCode.NotFound));
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                    return(Response.AsJson(new { error = $"Failed to remove cluster with id {id}: {ex.Message}" }, HttpStatusCode.InternalServerError));
                }
            });
        }