Esempio n. 1
0
        private void SetUpQueue(object queueType)
        {
            Console.WriteLine("nu startes :" + (string)queueType);

            string jsonstring = "";

            var connection = factory.CreateConnection();
            var channel    = connection.CreateModel();


            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            Console.WriteLine(" [*] Waiting for messages.");

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, ea) =>
            {
                var body    = ea.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine("besked modtaget [x] Received {0}", message);
                jsonstring = message;

                switch (queueType)
                {
                case "customercreate_queue":
                    DTOCustomer insertCustomer = Mapper.ConvertFromJsonToDTOCustomer(jsonstring);
                    database.InsertCustomerIntoDatabase(insertCustomer);
                    break;

                case "customerupdate_queue":
                    DTOCustomer upateCustomer = Mapper.ConvertFromJsonToDTOCustomer(jsonstring);
                    database.UpdateCustomerInDatabase(upateCustomer);
                    break;

                case "ressourceCreate_queue":
                    DTORessource createRessource = Mapper.ConvertFromJsonToDTOResource(jsonstring);
                    database.InsertRessourceInDataBase(createRessource);
                    break;

                case "ressourceUpdate_queue":
                    DTORessource updateRessource = Mapper.ConvertFromJsonToDTOResource(jsonstring);
                    database.UpdataRessourceInDataBase(updateRessource);
                    break;

                default:
                    // code block
                    break;
                }
                Console.WriteLine(" [x] Done");

                // Note: it is possible to access the channel via
                //       ((EventingBasicConsumer)sender).Model here
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            };
            channel.BasicConsume(queue: (string)queueType,
                                 autoAck: false,
                                 consumer: consumer);
        }
Esempio n. 2
0
        public void UpdataRessourceInDataBase(DTORessource resource)
        {
            //Opdater resource i databasen

            try
            {
                string query = $"update Ressources set " +
                               $"IsAvailable = @IsAvailable, " +
                               $"Modelstring = @Modelstring, " +
                               $"Price = @Price " +
                               $"where id = @id";

                using (SqlCommand command = new SqlCommand(query, conn))
                {
                    command.Parameters.AddWithValue("@id", resource.RessourceId);
                    command.Parameters.AddWithValue("@Modelstring", resource.ModelString);
                    command.Parameters.AddWithValue("@Price", resource.Price);
                    command.Parameters.AddWithValue("@IsAvailable", resource.IsAvailable);

                    conn.Open();
                    int result = command.ExecuteNonQuery();
                    conn.Close();

                    // Check Error
                    if (result < 0)
                    {
                        Console.WriteLine("Error inserting data into Database!");
                    }
                    else
                    {
                        Console.WriteLine("nu er der vistnok skrevet en ressourse i databasen");
                    }
                }
            }
            catch
            {
                Console.WriteLine("Ressourse ikke opdateret i SQL-databasen");
            }
        }
Esempio n. 3
0
        public void InsertRessourceInDataBase(DTORessource resource)
        {
            //skriv en rosurce ind i databasen

            try
            {
                string query = $"insert into Ressources(Id, Modelstring, isAvailable,  Price) " +
                               $"values(@id, @modelstring, @isAvailable,  @price)";

                using (SqlCommand command = new SqlCommand(query, conn))
                {
                    command.Parameters.AddWithValue("@id", resource.RessourceId);
                    command.Parameters.AddWithValue("@modelstring", resource.ModelString);
                    command.Parameters.AddWithValue("@isAvailable", resource.IsAvailable);
                    command.Parameters.AddWithValue("@price", resource.Price);

                    conn.Open();
                    int result = command.ExecuteNonQuery();
                    conn.Close();

                    // Check Error
                    if (result < 0)
                    {
                        Console.WriteLine("Error inserting data into Database!");
                    }
                    else
                    {
                        Console.WriteLine("nu er der vistnok skrevet en resurse i databasen");
                    }
                }
            }
            catch
            {
                Console.WriteLine("Ressurse ikke skrevet ind i SQL-databasen");
            }
        }