public async Task <IActionResult> PutServer([FromRoute] string id, [FromBody] Server.API.Models.Server server)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != server.Name)
            {
                return(BadRequest());
            }

            _context.Entry(server).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ServerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostServer([FromBody] Server.API.Models.Server server)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Servers.Add(server);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetServer", new { id = server.Name }, server));
        }
示例#3
0
        public static ServerViewModel Create(Models.Server server)
        {
            if (server == null)
            {
                throw new ArgumentNullException(nameof(server));
            }
            ServerViewModel sv = new ServerViewModel()
            {
                Id          = server.Id,
                Name        = server.Name,
                IPv4        = server.IPv4,
                Domain      = server.Domain,
                UserName    = server.UserName,
                Description = server.Description,
                IsHybrid    = server.IsHybrid,
                IsPublic    = server.IsPublic,
                Protocals   = server.ServerProtocals?.Select(c => c.ProtocalId).ToArray(),
                Off         = server.Off
            };

            if (server.IsHybrid)
            {
                sv.RedirectorServerName        = server.RedirectorServer?.Name;
                sv.RedirectorServerCountryName = server.RedirectorServer?.Country?.Name;
                sv.RedirectorServerCountryFlag = server.RedirectorServer?.Country?.Flag;

                sv.TrafficServerName        = server.TrafficServer?.Name;
                sv.TrafficServerCountryName = server.TrafficServer?.Country?.Name;
                sv.TrafficServerCountryFlag = server.TrafficServer?.Country?.Flag;

                if (!String.IsNullOrEmpty(server.TrafficServer?.HealthReportJson))
                {
                    sv.HealthReport = JsonConvert.DeserializeObject <HealthReport>(server.TrafficServer.HealthReportJson);
                }
            }
            else
            {
                sv.CountryName = server.Country?.Name;
                sv.CountryFlag = server.Country?.Flag;

                if (!String.IsNullOrEmpty(server.HealthReportJson))
                {
                    sv.HealthReport = JsonConvert.DeserializeObject <HealthReport>(server.HealthReportJson);
                }
            }

            return(sv);
        }