Пример #1
0
        public IActionResult Update(ServerUpdateRequestModel model)
        {
            try
            {
                // Check existed server name
                var existServer = _repo.GetActive(p => p.Name == model.Name && p.Id != model.Id).FirstOrDefault();
                if (existServer != null)
                {
                    return(BadRequest(new { error = "Server Name is existed!" }));
                }
                //  Check Server Master
                if (model.ServerMaster != null)
                {
                    ServerFilter filter = new ServerFilter()
                    {
                        ids = (int)model.ServerMaster + ""
                    };
                    if (_service.Get(filter).FirstOrDefault() == null)
                    {
                        return(BadRequest(new { error = "Server Master is not existed!" }));
                    }
                }

                if ((model.Type == 1 || model.Type == 2) && model.Os > 2)
                {
                    return(BadRequest(new { error = "Web Server only be Windows or Linux" }));
                }
                if (model.Type == 3 && (model.Os < 3 || model.Os > 6))
                {
                    return(BadRequest(new { error = "Repo Server - Server Os should be between 3 and 6" }));
                }
                if (model.Type == 4 && (model.Os < 7 || model.Os > 10))
                {
                    return(BadRequest(new { error = "Database Server - Server Os should be between 7 and 10" }));
                }
                if (model.IpAddress != null && !IPAddress.TryParse(model.IpAddress, out IPAddress address))
                {
                    return(BadRequest(new { error = "IpAdress is wrong format!" }));
                }
                var result = _service.Update(model);
                if (model == null)
                {
                    BadRequest(new { error = "Invalid Server" });
                }

                //  Update ServerDetail
                if (model.ServerDetail != null)
                {
                    var updateServerDetail = _serverDetailService.Update(model.ServerDetail);
                    if (updateServerDetail == null)
                    {
                        return(BadRequest("Server updated but server detail is invalid"));
                    }
                }

                return(Ok(result));
            }
            catch (System.Exception e)
            {
                try { _logService.SendLogError(e); } catch (System.Exception ex) { return(StatusCode(503, ex.Message)); }
                return(StatusCode(503, e));
            }
        }