public void ProduceHourRegistrationMessage(HourRegistrationModel registeredHours) { var factory = new ConnectionFactory() { HostName = "rabbitmq" }; using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "IncomingHourRegistrationMessages", durable: false, exclusive: false, autoDelete: false, arguments: null); string message = registeredHours.ToString(); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "IncomingHourRegistrationMessages", basicProperties: null, body: body); Console.WriteLine($"Following message was sent to queue: {message}"); } }
public bool DataIsValid(HourRegistrationModel registeredHours) { bool valid = false; try { LocalDate startDate = LocalDate.Parse($"{registeredHours.StartTime.Year}-{registeredHours.StartTime.Month}-{registeredHours.StartTime.Day}"); LocalDate endDate = LocalDate.Parse($"{registeredHours.EndTime.Year}-{registeredHours.EndTime.Month}-{registeredHours.EndTime.Day}"); LocalTime startTime = LocalTime.Parse($"{registeredHours.StartTime.Hour}:{registeredHours.StartTime.Minute}:{registeredHours.StartTime.Second}"); LocalTime endTime = LocalTime.Parse($"{registeredHours.EndTime.Hour}:{registeredHours.EndTime.Minute}:{registeredHours.EndTime.Second}"); Guid emp = Guid.Parse(registeredHours.EmployeeId); Guid com = Guid.Parse(registeredHours.CompanyId); Guid proj = Guid.Parse(registeredHours.ProjectId); if (registeredHours.StartTime < registeredHours.EndTime) { valid = true; } } catch (Exception e) { Console.WriteLine($"The format was not correct! {e.Message}"); } return(valid); }
public void DataIsValidFalseForInvalidData(int indexOfInvalidDataList) { //Arrange HourRegistrationModel model = _invalidModelsList[indexOfInvalidDataList]; //Act + Assert Assert.False(_producer.DataIsValid(model)); }
public void DataIsValidTrueForValidData(int indexOfValidDataList) { //Arrange HourRegistrationModel model = _validModelsList[indexOfValidDataList]; //Act + Assert Assert.True(_producer.DataIsValid(model)); }
public void RegisterHour(string projectId, string activityId, string regId, int hours, string shortDescription) { var userId = uManager.loggedIn().ModelIdentity; var parentActivity = pManager.ActivityModelById(projectId, activityId); var rObject = new HourRegistrationModel(regId, hours, userId, shortDescription, parentActivity); }
static public void Main() { var factory = new ConnectionFactory() { HostName = "rabbitmq" }; factory.AutomaticRecoveryEnabled = true; IConnection connection = GetRabbitMqConnection(factory); Cluster cluster = Cluster.Builder() .AddContactPoint("cassandra") .Build(); using (connection) { using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "IncomingHourRegistrationMessages", durable: false, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); HourRegistrationModel retrievedModel = GetModelFromBody(body); ISession cassandraSession = GetCassandraSession(cluster); var preparedStatement = cassandraSession.Prepare("INSERT INTO uurtjefactuurtje.registered_hours_by_employee (company_id, project_id, employee_id, start_date, start_time, end_date, end_time, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); WriteToDatabase(retrievedModel, cassandraSession, preparedStatement); Console.WriteLine(retrievedModel.Description); }; channel.BasicConsume(queue: "IncomingHourRegistrationMessages", autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }
public bool ProduceHourRegistrationMessage(HourRegistrationModel registeredHours) { if (!DataIsValid(registeredHours)) { return(false); } if (!GetConnection(_factory, 30)) { return(false); } if (!SendMessage(registeredHours)) { return(false); } return(true); }
private bool SendMessage(HourRegistrationModel registeredHours) { bool messageWasWritten = false; try { using (_connection) using (var channel = _connection.CreateModel()) { channel.QueueDeclare(queue: "IncomingHourRegistrationMessages", durable: false, exclusive: false, autoDelete: false, arguments: null); HourRegistrationModel message = registeredHours; IFormatter formatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); formatter.Serialize(memoryStream, message); var body = memoryStream.ToArray(); channel.BasicPublish(exchange: "", routingKey: "IncomingHourRegistrationMessages", basicProperties: null, body: body); Console.WriteLine("Following message was sent to queue:"); Console.WriteLine(message.Description); messageWasWritten = true; } } catch (Exception e) { Console.WriteLine($"Exception was caught when sending a message to the queue: {e.Message}"); messageWasWritten = false; } return(messageWasWritten); }
private void button1_Click(object sender, EventArgs e) { var currentUserId = service.CurrentUserLoggedIn().ModelIdentity; string regTitle = TitleBoxSelector.Text, activityId = ActivityComboBoxSelector.Text, description = DescriptionBoxSelector.Text, Hours = HourBoxSelector.Text; if (!int.TryParse(Hours, out int hours)) { throw new ArgumentException("Something went wrong with conversion from string to int."); } var ParentActivity = service.Activity(activityId); var rObject = new HourRegistrationModel(regTitle, hours, currentUserId, description, ParentActivity); var sEvent = new SubmitEvent(rObject); Close(); }
static bool WriteToDatabase(HourRegistrationModel model, ISession session, PreparedStatement preparedStatement) { bool success = true; try { LocalDate startDate = LocalDate.Parse($"{model.StartTime.Year}-{model.StartTime.Month}-{model.StartTime.Day}"); LocalDate endDate = LocalDate.Parse($"{model.EndTime.Year}-{model.EndTime.Month}-{model.EndTime.Day}"); LocalTime startTime = LocalTime.Parse($"{model.StartTime.Hour}:{model.StartTime.Minute}:{model.StartTime.Second}"); LocalTime endTime = LocalTime.Parse($"{model.EndTime.Hour}:{model.EndTime.Minute}:{model.EndTime.Second}"); Guid emp = Guid.Parse(model.EmployeeId); Guid com = Guid.Parse(model.CompanyId); Guid proj = Guid.Parse(model.ProjectId); var statement = preparedStatement.Bind(com, proj, emp, startDate, startTime, endDate, endTime, model.Description); session.ExecuteAsync(statement); } catch (Exception e) { Console.WriteLine($"Writing message to database was not successfull, following error was thrown: {e.Message}"); success = false; } return(success); }
public void AddValidModels() { var model = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), ProjectId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now }; var model1 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), ProjectId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now, Description = "Valid description" }; _validModelsList.Add(model); _validModelsList.Add(model1); }
public void AddInvalidModels() { var model = new HourRegistrationModel { CompanyId = "invalid", EmployeeId = Guid.NewGuid().ToString(), ProjectId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now, Description = "Valid description" }; var model1 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = "invalid", ProjectId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now, Description = "Valid description" }; var model2 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), ProjectId = "invalid", StartTime = DateTime.Now, EndTime = DateTime.Now, Description = "Valid description" }; var model3 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), ProjectId = Guid.NewGuid().ToString(), Description = "Valid description" }; var model4 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now, Description = "Valid description" }; var model5 = new HourRegistrationModel { CompanyId = Guid.NewGuid().ToString(), EmployeeId = Guid.NewGuid().ToString(), ProjectId = Guid.NewGuid().ToString(), StartTime = DateTime.Now, EndTime = DateTime.Now.AddMonths(-1), Description = "Valid description" }; _invalidModelsList.Add(model); _invalidModelsList.Add(model1); _invalidModelsList.Add(model2); _invalidModelsList.Add(model3); _invalidModelsList.Add(model4); _invalidModelsList.Add(model5); }