Created
March 31, 2017 18:41
-
-
Save marcioeric/a31486198589733cd804b03f53cf222d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using System.IO; | |
using System.Net; | |
namespace rdi_server.service | |
{ | |
public class Connector | |
{ | |
private string Host { get; } | |
private WebRequest _request; | |
public WebResponse Response; | |
public string ConnectionStatus; | |
private Stream _dataStream; | |
private StreamReader _reader; | |
public Connector(string config) | |
{ | |
Host = ConfigurationManager.AppSettings[config]; | |
} | |
public string GetData() | |
{ | |
_request = WebRequest.Create(Host); | |
_request.Credentials = CredentialCache.DefaultCredentials; | |
Response = _request.GetResponse(); | |
ConnectionStatus = (((HttpWebResponse)Response).StatusDescription); | |
_dataStream = Response.GetResponseStream(); | |
_reader = new StreamReader(_dataStream); | |
var toReturn = _reader.ReadToEnd(); | |
_reader.Close(); | |
Response.Close(); | |
return toReturn; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.SqlClient; | |
using rdi_musica.core; | |
namespace rdi_server.service | |
{ | |
public static class DomainDAO | |
{ | |
public static int InsertUsuario(SqlConnection connection, Usuario usuario) | |
{ | |
var qry = | |
String.Format( | |
"IF (NOT EXISTS(SELECT * FROM Usuario WHERE Id = {0})) BEGIN INSERT INTO Usuario(Nome, Email, Senha, DtCadastro, Ativo) VALUES( '{1}', '{2}', '{3}', convert(datetime,'{4}',105), '{5}') END ELSE BEGIN UPDATE Usuario SET Nome = '{1}', Email = '{2}', Senha = '{3}', DtCadastro = convert(datetime,'{4}',105), Ativo = '{5}' WHERE Id = {0} END", | |
usuario.Id, usuario.Nome, usuario.Email, usuario.Senha, usuario.DtCadastro.ToString("g"), usuario.Ativo); | |
var query = new SqlCommand(qry ,connection); | |
return query.ExecuteNonQuery(); | |
} | |
public static int InsertGenero(SqlConnection connection, Genero genero) | |
{ | |
var qry = | |
String.Format( | |
"IF (NOT EXISTS(SELECT * FROM Genero WHERE Id = {0})) BEGIN INSERT INTO Genero(Nome, Descricao, CriadorId, DtCriacao, Ativo) VALUES( '{1}', '{2}', '{3}', convert(datetime,'{4}',105), '{5}') END ELSE BEGIN UPDATE Genero SET Nome = '{1}', Descricao = '{2}', CriadorId = '{3}', DtCriacao = convert(datetime,'{4}',105), Ativo = '{5}' WHERE Id = {0} END", | |
genero.Id, genero.Nome, genero.Descricao, genero.CriadorId, genero.DtCriacao.ToString("g"), genero.Ativo); | |
var query = new SqlCommand(qry, connection); | |
return query.ExecuteNonQuery(); | |
} | |
public static int InsertBanda(SqlConnection connection, Banda banda) | |
{ | |
var qry = | |
String.Format( | |
"IF (NOT EXISTS(SELECT * FROM Banda WHERE Id = {0})) BEGIN INSERT INTO Banda(Nome, Descricao, CriadorId, DtCriacao, Ativo) VALUES( '{1}', '{2}', '{3}', convert(datetime,'{4}',105), '{5}') END ELSE BEGIN UPDATE Banda SET Nome = '{1}', Descricao = '{2}', CriadorId = '{3}', DtCriacao = convert(datetime,'{4}',105), Ativo = '{5}' WHERE Id = {0} END", | |
banda.Id, banda.Nome, banda.Descricao, banda.CriadorId, banda.DtCriacao.ToString("g"), banda.Ativo); | |
var query = new SqlCommand(qry, connection); | |
return query.ExecuteNonQuery(); | |
} | |
public static int InsertMusica(SqlConnection connection, Musica musica) | |
{ | |
var qry = | |
String.Format( | |
"IF (NOT EXISTS(SELECT * FROM Musica WHERE Id = {0})) BEGIN INSERT INTO Musica(Titulo, BandaId, Link, DtCriacao, GeneroId, CriadorId, Ativo) VALUES( '{1}', {2}, '{3}', convert(datetime,'{4}',105), {5} , {6}, '{7}') END ELSE BEGIN UPDATE Musica SET Titulo = '{1}', BandaId = {2}, Link = '{3}', DtCriacao = convert(datetime,'{4}',105), Ativo = '{7}', GeneroId = {5}, CriadorId = {6} WHERE Id = {0} END", | |
musica.Id, musica.Titulo, musica.BandaId, musica.Link, musica.DtCriacao.ToString("g"), musica.GeneroId, musica.CriadorId, musica.Ativo); | |
var query = new SqlCommand(qry, connection); | |
return query.ExecuteNonQuery(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using rdi_musica.core; | |
namespace rdi_server.service | |
{ | |
public class Loader | |
{ | |
public static List<Usuario> LoadUsuarios(Connector usuarioConnector) | |
{ | |
return (List<Usuario>)Newtonsoft.Json.JsonConvert.DeserializeObject(usuarioConnector.GetData(), typeof(List<Usuario>)); | |
} | |
public static List<Banda> LoadBandas(Connector bandaConnector) | |
{ | |
var bandasDtos = (List<BandaDto>)Newtonsoft.Json.JsonConvert.DeserializeObject(bandaConnector.GetData(), typeof(List<BandaDto>)); | |
return bandasDtos.Select(dto => dto.ConvertToBanda()).ToList(); | |
} | |
public static List<Genero> LoadGeneros(Connector generoConnector) | |
{ | |
var musicasDtos = (List<GeneroDto>)Newtonsoft.Json.JsonConvert.DeserializeObject(generoConnector.GetData(), typeof(List<GeneroDto>)); | |
return musicasDtos.Select(dto => dto.ConvertToGenero()).ToList(); | |
} | |
public static List<Musica> LoadMusicas(Connector musicaConnector) | |
{ | |
var musicasDtos = (List<MusicaDto>)Newtonsoft.Json.JsonConvert.DeserializeObject(musicaConnector.GetData(), typeof(List<MusicaDto>)); | |
return musicasDtos.Select(dto => dto.ConvertToMusica()).ToList(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ServiceProcess; | |
namespace rdi_server.service | |
{ | |
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
static void Main() | |
{ | |
#if(!DEBUG) | |
ServiceBase[] ServicesToRun; | |
ServicesToRun = new ServiceBase[] | |
{ | |
new Service1() | |
}; | |
ServiceBase.Run(ServicesToRun); | |
#else | |
Service1 service = new Service1(); | |
service.StartDebug(); | |
#endif | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.ServiceProcess; | |
using System.Threading; | |
namespace rdi_server.service | |
{ | |
public partial class Service1 : ServiceBase | |
{ | |
private Thread _thread; | |
private Updater _updater = new Updater(); | |
private readonly int Interval = Convert.ToInt32(ConfigurationManager.AppSettings["timer"]); | |
private readonly string Connection = ConfigurationManager.AppSettings["connection"]; | |
private readonly Connector _usuarioConnector; | |
private readonly Connector _bandaConnector; | |
private readonly Connector _generoConnector; | |
private readonly Connector _musicaConnector; | |
public Service1() | |
{ | |
_usuarioConnector = new Connector("UsuarioBase"); | |
_bandaConnector = new Connector("BandaBase"); | |
_generoConnector = new Connector("GeneroBase"); | |
_musicaConnector = new Connector("MusicaBase"); | |
InitializeComponent(); | |
} | |
protected override void OnStart(string[] args) | |
{ | |
StartDebug(); | |
} | |
public void StartDebug() | |
{ | |
_thread = new Thread(OnTime); | |
_thread.Name = "My Worker Thread"; | |
_thread.Start(); | |
} | |
protected override void OnStop() | |
{ | |
} | |
protected void OnTime() | |
{ | |
while (true) | |
{ | |
EventLog.WriteEntry("Dentro do WHILE foi executado!"); | |
_updater.Do(Connection, _usuarioConnector, _generoConnector, _bandaConnector, _musicaConnector); | |
EventLog.WriteEntry("Fim do while"); | |
Thread.Sleep(Interval); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using rdi_musica.core; | |
using System.Collections.Generic; | |
using System.Data.SqlClient; | |
namespace rdi_server.service | |
{ | |
class Updater | |
{ | |
public void Do(string Connection, | |
Connector usuario, | |
Connector genero, | |
Connector banda, | |
Connector musica) | |
{ | |
var usuarios = Loader.LoadUsuarios(usuario); | |
var generos = Loader.LoadGeneros(genero); | |
var bandas = Loader.LoadBandas(banda); | |
var musicas = Loader.LoadMusicas(musica); | |
using (var connection = new SqlConnection(Connection)) | |
{ | |
connection.Open(); | |
foreach (var _usuario in usuarios) | |
{ | |
DomainDAO.InsertUsuario(connection, _usuario); | |
} | |
foreach (var _genero in generos) | |
{ | |
DomainDAO.InsertGenero(connection, _genero); | |
} | |
foreach (var _banda in bandas) | |
{ | |
DomainDAO.InsertBanda(connection, _banda); | |
} | |
foreach (var _musica in musicas) | |
{ | |
DomainDAO.InsertMusica(connection, _musica); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment