public async Task InitializeServersData() { if (initialized) { return; } var serversData = JsonConvert.DeserializeObject <List <ServerData> >(File.ReadAllText(this.dataFilePath)); foreach (var serverData in serversData) { try { var server = context.ServersData.SingleOrDefault(sd => sd.Name == serverData.Name); if (server == null) { context.ServersData.Add(serverData); await context.SaveChangesAsync(); } } catch (Exception) { } } initialized = true; }
public async Task <PostServerResponse> AddServerAsync([FromBody] ServerData server) { var response = new PostServerResponse(); var existingServer = context.ServersData.SingleOrDefault(sd => sd.Name.Trim().ToLower() == server.Name.Trim().ToLower() || sd.WebUrl.Trim().ToLower() == server.WebUrl.Trim().ToLower() || sd.UsersEndpoint.Trim().ToLower() == server.UsersEndpoint.Trim().ToLower()); if (existingServer != null) { var errorMessage = string.Empty; response.HasError = true; response.IsNameRepeated = existingServer.Name.Trim().ToLower() == server.Name.Trim().ToLower(); response.IsUsersEndpointRepeated = existingServer.UsersEndpoint.Trim().ToLower() == server.UsersEndpoint.Trim().ToLower(); response.IsWebRepeated = existingServer.WebUrl.Trim().ToLower() == server.WebUrl.Trim().ToLower(); } response.WebIsNotValid = !await ValidURL(server.WebUrl); response.UsersEndpointIsNotValid = !await ValidURL(server.UsersEndpoint); if (response.WebIsNotValid || response.UsersEndpointIsNotValid) { response.HasError = true; } if (!response.HasError) { context.ServersData.Add(server); await context.SaveChangesAsync(); response.ServerData = server; } return(response); }