Komplette Überarbeitung - Import technisch richtig / File wird noch nicht gefunden
This commit is contained in:
parent
9f2e9cbec8
commit
26c62d000d
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,12 +1,37 @@
|
|||||||
{
|
{
|
||||||
"Version": 1,
|
"Version": 1,
|
||||||
"WorkspaceRootPath": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\",
|
"WorkspaceRootPath": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\",
|
||||||
"Documents": [],
|
"Documents": [
|
||||||
|
{
|
||||||
|
"AbsoluteMoniker": "D:0:0:{44DD7752-6BB5-4C3A-9053-671D8ADE49C4}|Projekt_Calcan_Conze\\Projekt_Calcan_Conze.csproj|c:\\jan_bib_module\\pmc\\projekt\\projekt_calcan_conze\\projekt_calcan_conze\\programm.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
|
||||||
|
"RelativeMoniker": "D:0:0:{44DD7752-6BB5-4C3A-9053-671D8ADE49C4}|Projekt_Calcan_Conze\\Projekt_Calcan_Conze.csproj|solutionrelative:projekt_calcan_conze\\programm.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
|
||||||
|
}
|
||||||
|
],
|
||||||
"DocumentGroupContainers": [
|
"DocumentGroupContainers": [
|
||||||
{
|
{
|
||||||
"Orientation": 0,
|
"Orientation": 0,
|
||||||
"VerticalTabListWidth": 256,
|
"VerticalTabListWidth": 256,
|
||||||
"DocumentGroups": []
|
"DocumentGroups": [
|
||||||
|
{
|
||||||
|
"DockedWidth": 200,
|
||||||
|
"SelectedChildIndex": 0,
|
||||||
|
"Children": [
|
||||||
|
{
|
||||||
|
"$type": "Document",
|
||||||
|
"DocumentIndex": 0,
|
||||||
|
"Title": "Programm.cs",
|
||||||
|
"DocumentMoniker": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze\\Programm.cs",
|
||||||
|
"RelativeDocumentMoniker": "Projekt_Calcan_Conze\\Programm.cs",
|
||||||
|
"ToolTip": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze\\Programm.cs",
|
||||||
|
"RelativeToolTip": "Projekt_Calcan_Conze\\Programm.cs",
|
||||||
|
"ViewState": "AgIAAAAAAAAAAAAAAAAAABYAAAApAAAAAAAAAA==",
|
||||||
|
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
|
||||||
|
"WhenOpened": "2024-08-23T08:36:46.981Z",
|
||||||
|
"EditorCaption": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
12
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Constants.cs
Normal file
12
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Constants.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace Projekt_Calcan_Conze;
|
||||||
|
|
||||||
|
internal static class Constants
|
||||||
|
{
|
||||||
|
public const char Separator = ';';
|
||||||
|
public const string FemaleAttributeIdentifier = "Frau";
|
||||||
|
public const string MaleAttributeIdentifier = "Herr";
|
||||||
|
public const string AddressAttributeIdentifier = "Adresse";
|
||||||
|
public const string EmailAttributeIdentifier = "E-Mail";
|
||||||
|
public const string PhoneNumberAttributeIdentifier = "Telefon";
|
||||||
|
public const string DateOfBirthFormat = "dd.MM.yyyy";
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
namespace Projekt_Calcan_Conze.DTOs;
|
||||||
|
|
||||||
|
internal class CustomerAttributeDto
|
||||||
|
{
|
||||||
|
public CustomerAttributeDto(List<string> parts)
|
||||||
|
{
|
||||||
|
this.Parts = parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> Parts { get; }
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
using Projekt_Calcan_Conze.DTOs;
|
||||||
|
|
||||||
|
namespace Projekt_Calcan_Conze.DTOs;
|
||||||
|
|
||||||
|
internal class CustomerDto
|
||||||
|
{
|
||||||
|
public CustomerDto(List<CustomerAttributeDto> attributes)
|
||||||
|
{
|
||||||
|
this.Attributes = attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CustomerAttributeDto> Attributes { get; }
|
||||||
|
}
|
17
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Models/Address.cs
Normal file
17
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Models/Address.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
namespace Projekt_Calcan_Conze.Models;
|
||||||
|
|
||||||
|
internal class Address
|
||||||
|
{
|
||||||
|
public Address(string streetAndHouseNumber, string postalCode, string city)
|
||||||
|
{
|
||||||
|
this.StreetAndHouseNumber = streetAndHouseNumber;
|
||||||
|
this.PostalCode = postalCode;
|
||||||
|
this.City = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string StreetAndHouseNumber { get; }
|
||||||
|
|
||||||
|
public string PostalCode { get; }
|
||||||
|
|
||||||
|
public string City { get; }
|
||||||
|
}
|
65
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Models/Customer.cs
Normal file
65
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Models/Customer.cs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
using Projekt_Calcan_Conze.Models;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Projekt_Calcan_Conze.Models;
|
||||||
|
|
||||||
|
internal class Customer
|
||||||
|
{
|
||||||
|
public Customer(
|
||||||
|
string title,
|
||||||
|
string firstName,
|
||||||
|
string lastName,
|
||||||
|
DateOnly dateOfBirth,
|
||||||
|
string? email,
|
||||||
|
Address? address,
|
||||||
|
List<PhoneNumber> phoneNumbers)
|
||||||
|
{
|
||||||
|
this.Title = title;
|
||||||
|
this.FirstName = firstName;
|
||||||
|
this.LastName = lastName;
|
||||||
|
this.DateOfBirth = dateOfBirth;
|
||||||
|
this.Email = email;
|
||||||
|
this.Address = address;
|
||||||
|
this.PhoneNumbers = phoneNumbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Title { get; }
|
||||||
|
|
||||||
|
public string FirstName { get; }
|
||||||
|
|
||||||
|
public string LastName { get; }
|
||||||
|
|
||||||
|
public DateOnly DateOfBirth { get; }
|
||||||
|
|
||||||
|
public string? Email { get; }
|
||||||
|
|
||||||
|
public Address? Address { get; }
|
||||||
|
|
||||||
|
public List<PhoneNumber> PhoneNumbers { get; }
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
StringBuilder builder = new();
|
||||||
|
|
||||||
|
builder.Append($"{this.Title} {this.FirstName} {this.LastName}");
|
||||||
|
builder.Append($"{Constants.Separator} {this.DateOfBirth.ToString(Constants.DateOfBirthFormat)}");
|
||||||
|
|
||||||
|
if (this.Email is not null)
|
||||||
|
{
|
||||||
|
builder.Append($"{Constants.Separator} {this.Email}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.Address is not null)
|
||||||
|
{
|
||||||
|
builder.Append($"{Constants.Separator} {this.Address.StreetAndHouseNumber}");
|
||||||
|
builder.Append($"{Constants.Separator} {this.Address.PostalCode} {this.Address.City}");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (PhoneNumber phoneNumber in this.PhoneNumbers)
|
||||||
|
{
|
||||||
|
builder.Append($"{Constants.Separator} {phoneNumber.AreaCode} {phoneNumber.Number}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.ToString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
namespace Projekt_Calcan_Conze.Models;
|
||||||
|
|
||||||
|
internal class PhoneNumber
|
||||||
|
{
|
||||||
|
public PhoneNumber(string areaCode, string number)
|
||||||
|
{
|
||||||
|
this.AreaCode = areaCode;
|
||||||
|
this.Number = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string AreaCode { get; }
|
||||||
|
|
||||||
|
public string Number { get; }
|
||||||
|
}
|
36
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Programm.cs
Normal file
36
Projekt_Calcan_Conze/Projekt_Calcan_Conze/Programm.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using Projekt_Calcan_Conze;
|
||||||
|
using Projekt_Calcan_Conze.Models;
|
||||||
|
|
||||||
|
string? filePath = null;
|
||||||
|
|
||||||
|
while (filePath is null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Bitte gib einen Dateipfad an:");
|
||||||
|
filePath = Console.ReadLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
string? customerId = null;
|
||||||
|
|
||||||
|
while (customerId is null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Bitte gib eine Kundennummer an:");
|
||||||
|
customerId = Console.ReadLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
var (customers, protocol) = Import.For(filePath);
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine("Importierte Kunden:");
|
||||||
|
|
||||||
|
foreach (Customer customer in customers)
|
||||||
|
{
|
||||||
|
Console.WriteLine(customer.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine("Ungültige nicht importierte Zeilen/Kunden:");
|
||||||
|
|
||||||
|
foreach (string protocolLine in protocol)
|
||||||
|
{
|
||||||
|
Console.WriteLine(protocolLine);
|
||||||
|
}
|
@ -1,113 +0,0 @@
|
|||||||
// 2. Export
|
|
||||||
//
|
|
||||||
// • Ein Sachbearbeiter ruft eine Konsolen-App auf, in welcher er die Kundennummer sowie den Zeitraum für die Abrechnung angibt
|
|
||||||
// • Wird eine Aktion abgerechnet, darf diese zukünftig nicht nochmal berechnet werden (z.B. bei Überschneidung der Zeiträume)
|
|
||||||
// • Die Ausgabe der Daten erfolgt als CSV-Datei. Der Dateiname folgt diesem Schema: Abrechnungsdatum_Kundennummer_vonDatum_bisDatum.csv
|
|
||||||
// • Das Datum ist immer im folgenden Format anzugeben: yyyy-mm-dd
|
|
||||||
// • Ein Abrechnungslauf muss folgende Daten umfassen:
|
|
||||||
// ○ Zeitraum (von / bis Datum) der Abrechnung
|
|
||||||
// ○ Name und Nummer des Kunden
|
|
||||||
// ○ Gesamtsumme der Abrechnung
|
|
||||||
// ○ Übersicht der einzelnen Posten wie folgt:
|
|
||||||
// Bereich | Grundbetrag | Anzahl | Betrag
|
|
||||||
// Newsletter wöchentlich | 0.001 | 24 | 0.024
|
|
||||||
// Tarif Orange M 12 Monate | 7.00 | 0 | 0.000
|
|
||||||
// Tarif Orange S 12 Monate | 5.00 | 1 | 5.000
|
|
||||||
// Tarif Orange XL 12 Monate | 12.00 | 2 | 24.000
|
|
||||||
// Verkaufsgespräch | 0.030 | 8 | 0.240
|
|
||||||
//
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
|
|
||||||
class Export
|
|
||||||
{
|
|
||||||
static void Main(string[] args)
|
|
||||||
{
|
|
||||||
string customerNumber = args[0];
|
|
||||||
string startDatum = args[1];
|
|
||||||
string endDatum = args[2];
|
|
||||||
|
|
||||||
ExportData(customerNumber, startDatum, endDatum);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ExportData(string kundennummer, string startDatum, string endDatum)
|
|
||||||
{
|
|
||||||
string myConnectionString = "server=localhost;uid=root;pwd=root;database=vr_contact";
|
|
||||||
using (MySqlConnection myConnection = new MySqlConnection(myConnectionString))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
myConnection.Open();
|
|
||||||
|
|
||||||
// Kundendaten abrufen
|
|
||||||
string clientQuery = "SELECT name, id FROM customers WHERE id = @clientId";
|
|
||||||
MySqlCommand clientCmd = new MySqlCommand(clientQuery, myConnection);
|
|
||||||
clientCmd.Parameters.AddWithValue("@clientId", kundennummer);
|
|
||||||
|
|
||||||
using (MySqlDataReader clientReader = clientCmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
if (clientReader.Read())
|
|
||||||
{
|
|
||||||
string clientName = clientReader.GetString("name");
|
|
||||||
int clientId = clientReader.GetInt32("id");
|
|
||||||
clientReader.Close();
|
|
||||||
|
|
||||||
// Abrechnungsdaten abrufen
|
|
||||||
string invoiceQuery = @"
|
|
||||||
SELECT area, base_amount, quantity, amount
|
|
||||||
FROM invoices
|
|
||||||
WHERE client_id = @clientId
|
|
||||||
AND invoice_date BETWEEN @startDatum AND @endDatum";
|
|
||||||
|
|
||||||
MySqlCommand invoiceCmd = new MySqlCommand(invoiceQuery, myConnection);
|
|
||||||
invoiceCmd.Parameters.AddWithValue("@clientId", clientId);
|
|
||||||
invoiceCmd.Parameters.AddWithValue("@startDatum", startDatum);
|
|
||||||
invoiceCmd.Parameters.AddWithValue("@endDatum", endDatum);
|
|
||||||
|
|
||||||
using (MySqlDataReader invoiceReader = invoiceCmd.ExecuteReader())
|
|
||||||
{
|
|
||||||
List<string> invoiceLines = new List<string>();
|
|
||||||
decimal totalAmount = 0;
|
|
||||||
|
|
||||||
while (invoiceReader.Read())
|
|
||||||
{
|
|
||||||
string area = invoiceReader.GetString("area");
|
|
||||||
decimal baseAmount = invoiceReader.GetDecimal("base_amount");
|
|
||||||
int quantity = invoiceReader.GetInt32("quantity");
|
|
||||||
decimal amount = invoiceReader.GetDecimal("amount");
|
|
||||||
totalAmount += amount;
|
|
||||||
|
|
||||||
invoiceLines.Add($"{area};{baseAmount};{quantity};{amount}");
|
|
||||||
}
|
|
||||||
invoiceReader.Close();
|
|
||||||
|
|
||||||
// CSV-Datei schreiben
|
|
||||||
string filename = $"{DateTime.Now:yyyy-MM-dd}_{kundennummer}_{startDatum}_{endDatum}.csv";
|
|
||||||
using (StreamWriter sw = new StreamWriter(filename))
|
|
||||||
{
|
|
||||||
sw.WriteLine($"Zeitraum: {startDatum} bis {endDatum}");
|
|
||||||
sw.WriteLine($"Name und Nummer des Kunden: {clientName}, {clientId}");
|
|
||||||
sw.WriteLine($"Gesamtsumme der Abrechnung: {totalAmount}");
|
|
||||||
sw.WriteLine("Bereich;Grundbetrag;Anzahl;Betrag");
|
|
||||||
|
|
||||||
foreach (string line in invoiceLines)
|
|
||||||
{
|
|
||||||
sw.WriteLine(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
myConnection.Close();
|
|
||||||
}
|
|
||||||
catch (MySqlException ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Database error: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,243 +1,217 @@
|
|||||||
//Aufgabenstellung
|
using System.Net.Mail;
|
||||||
//
|
|
||||||
//1.Import
|
|
||||||
//
|
|
||||||
//Als Teil des Entwicklungsteams sollen Sie einen Import für neue Nutzer der jeweiligen Kunden entwickeln.
|
|
||||||
//Die Kunden schicken zu diesem Zweck CSV-Dateien mit den Daten von neuen Nutzern.
|
|
||||||
//Diese Daten müssen in die bestehenden Datentabellen eingespielt werden.
|
|
||||||
//Dafür sind folgende Anforderungen formuliert:
|
|
||||||
//
|
|
||||||
// • Ein Sachbearbeiter ruft eine Konsolen-App auf, in welcher er die Datei und die
|
|
||||||
// Kundennummer des Kunden als Parameter übergibt: Die Beispieldatei hat den Namen "import_yutani.csv" und
|
|
||||||
// gehört zum Kunden mit der Nummer "K2-002"
|
|
||||||
//
|
|
||||||
// • Die Datei hat den Aufbau:
|
|
||||||
// - Ein Nutzer mit Anrede, Vorname, Nachname und Geburtsdatum
|
|
||||||
// - Ggf. Adresse: Straße mit Hausnummer, PLZ, Stadt
|
|
||||||
// - Ggf. E-Mail
|
|
||||||
// - Ggf. eine oder mehrere Telefonnummern: Vorwahl ohne führende 0 und Nummer
|
|
||||||
//
|
|
||||||
// • Ein Nutzer darf für einen Kunden nicht mehrmals importiert werden.
|
|
||||||
//
|
|
||||||
// • Die Daten sollen wie folgt validiert werden:
|
|
||||||
// ○ Geburtsdatum: TT.MM.JJJJ
|
|
||||||
// ○ Valide E-Mail-Adresse
|
|
||||||
// ○ PLZ: exakt 5 Nummern
|
|
||||||
// ○ Telefonnummer 3 bis 5 Nummern für die Vorwahl und 4 bis 10 Nummern für die Hauptnummer. Keine Sonderzeichen
|
|
||||||
//
|
|
||||||
// • Nicht valide Datensätze von Nutzern sollen in einem Protokoll erfasst werden, ebenso bereits vorhandene Nutzer.
|
|
||||||
// Der komplette Datensatz eines Nutzers darf dann nicht importiert werden
|
|
||||||
|
|
||||||
/*
|
using Projekt_Calcan_Conze.DTOs;
|
||||||
* Das ist der Beispielcode für die Verbindung zum Datenbankserver
|
using Projekt_Calcan_Conze.Models;
|
||||||
*
|
using Microsoft.VisualBasic;
|
||||||
using MySql.Data;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
using MySqlX.XDevAPI;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Reflection.PortableExecutable;
|
|
||||||
using System.Xml;
|
|
||||||
|
|
||||||
|
namespace Projekt_Calcan_Conze;
|
||||||
|
|
||||||
MySql.Data.MySqlClient.MySqlConnection myConnection;
|
internal static class Import
|
||||||
string myConnectionString;
|
{
|
||||||
//set the correct values for your server, user, password and database name
|
public static (List<Customer> Customers, List<string> Protocol) For(string filePath)
|
||||||
myConnectionString = "server=localhost;uid=root;pwd=root;database=vr_contact";
|
{
|
||||||
|
var (customerDtos, protocol) = Import.ReadCustomerDtos(filePath);
|
||||||
|
var customers = Import.ParseCustomers(customerDtos, protocol);
|
||||||
|
return (customers, protocol);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (List<CustomerDto> CustomerDtos, List<string> Protocol) ReadCustomerDtos(
|
||||||
|
string filePath)
|
||||||
|
{
|
||||||
|
List<string> protocol = [];
|
||||||
|
List<CustomerDto> customerDtos = [];
|
||||||
|
List<string> lines;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
myConnection = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
|
lines =
|
||||||
//open a connection
|
File.ReadAllLines(filePath)
|
||||||
myConnection.Open();
|
.Where(line => !string.IsNullOrWhiteSpace(line)) // remove empty lines
|
||||||
|
.ToList();
|
||||||
// create a MySQL command and set the SQL statement with parameters
|
}
|
||||||
MySqlCommand myCommand = new MySqlCommand();
|
catch (Exception)
|
||||||
myCommand.Connection = myConnection;
|
|
||||||
myCommand.CommandText = @"SELECT * FROM workshop WHERE maxParticipants = @code;";
|
|
||||||
myCommand.Parameters.AddWithValue("@code", "12");
|
|
||||||
|
|
||||||
// execute the command and read the results
|
|
||||||
using MySqlDataReader myReader = myCommand.ExecuteReader();
|
|
||||||
|
|
||||||
while (myReader.Read())
|
|
||||||
{
|
{
|
||||||
int id = myReader.GetInt32("maxParticipants");
|
Console.WriteLine("The file does not exist or is currently used by another program.");
|
||||||
string name = myReader.GetString("title");
|
return (customerDtos, protocol);
|
||||||
Console.WriteLine(id + " " + name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
myConnection.Close();
|
CustomerDto? currentCustomer = null;
|
||||||
}
|
|
||||||
catch (MySql.Data.MySqlClient.MySqlException ex)
|
foreach (string line in lines)
|
||||||
{
|
{
|
||||||
|
var lineParts =
|
||||||
|
line.Split(Constants.Separator)
|
||||||
|
.Select(linePart => linePart.Trim())
|
||||||
|
.Where(linePart => !string.IsNullOrWhiteSpace(linePart)) // remove empty parts
|
||||||
|
.ToList(); // remove whitespace
|
||||||
|
|
||||||
}
|
string title = lineParts[0];
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
using System;
|
if (title is Constants.FemaleAttributeIdentifier or Constants.MaleAttributeIdentifier)
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using MySql.Data.MySqlClient;
|
|
||||||
|
|
||||||
class Import
|
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
if (currentCustomer is not null)
|
||||||
{
|
{
|
||||||
string filePath = args[0];
|
customerDtos.Add(currentCustomer);
|
||||||
string customerNumber = args[1];
|
|
||||||
|
|
||||||
ImportData(filePath, customerNumber);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ImportData(string filePath, string customerNumber)
|
currentCustomer = new CustomerDto([new CustomerAttributeDto(lineParts)]);
|
||||||
{
|
|
||||||
List<string> errorLog = new List<string>();
|
|
||||||
|
|
||||||
using (StreamReader sr = new StreamReader(filePath))
|
|
||||||
{
|
|
||||||
string line;
|
|
||||||
while ((line = sr.ReadLine()) != null)
|
|
||||||
{
|
|
||||||
string[] parts = line.Split(';');
|
|
||||||
User user = new User
|
|
||||||
{
|
|
||||||
Gender = parts[0],
|
|
||||||
Firstname = parts[1],
|
|
||||||
Lastname = parts[2],
|
|
||||||
Birthdate = parts[3],
|
|
||||||
Address = new Address
|
|
||||||
{
|
|
||||||
Street = parts[4],
|
|
||||||
PostalCode = parts[5],
|
|
||||||
City = parts[6]
|
|
||||||
},
|
|
||||||
Email = parts[7],
|
|
||||||
Phones = new List<Phone>
|
|
||||||
{
|
|
||||||
new Phone { Prefix = parts[8], Number = parts[9] }
|
|
||||||
}
|
}
|
||||||
};
|
else if (currentCustomer is not null)
|
||||||
|
|
||||||
if (ValidateUser(user, out string error))
|
|
||||||
{
|
{
|
||||||
InsertUser(user, customerNumber, errorLog);
|
currentCustomer.Attributes.Add(new CustomerAttributeDto(lineParts));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
errorLog.Add(error);
|
protocol.Add(line);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File.WriteAllLines("error_log.txt", errorLog);
|
if (currentCustomer is not null)
|
||||||
}
|
|
||||||
|
|
||||||
static bool ValidateUser(User user, out string error)
|
|
||||||
{
|
{
|
||||||
error = "";
|
customerDtos.Add(currentCustomer);
|
||||||
|
|
||||||
if (!Regex.IsMatch(user.Birthdate, @"^\d{2}\.\d{2}\.\d{4}$"))
|
|
||||||
{
|
|
||||||
error = "Invalid birthdate format.";
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(user.Email) && !Regex.IsMatch(user.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
|
return (customerDtos, protocol);
|
||||||
{
|
|
||||||
error = "Invalid email format.";
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Regex.IsMatch(user.Address.PostalCode, @"^\d{5}$"))
|
private static List<Customer> ParseCustomers(List<CustomerDto> customerDtos,
|
||||||
|
List<string> protocol)
|
||||||
{
|
{
|
||||||
error = "Invalid postal code format.";
|
List<Customer> customers = [];
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var phone in user.Phones)
|
foreach (CustomerDto customerDto in customerDtos)
|
||||||
{
|
{
|
||||||
if (!Regex.IsMatch(phone.Prefix, @"^\d{3,5}$") || !Regex.IsMatch(phone.Number, @"^\d{4,10}$"))
|
Customer? customer = Import.ParseCustomer(customerDto);
|
||||||
|
|
||||||
|
if (customer is not null)
|
||||||
{
|
{
|
||||||
error = "Invalid phone number format.";
|
customers.Add(customer);
|
||||||
return false;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Import.LogInvalidCustomer(protocol, customerDto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return customers;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void InsertUser(User user, string customerNumber, List<string> errorLog)
|
private static Customer? ParseCustomer(CustomerDto customer)
|
||||||
{
|
{
|
||||||
string myConnectionString = "server=localhost;uid=root;pwd=root;database=vr_contact";
|
CustomerAttributeDto firstAttribute = customer.Attributes[0];
|
||||||
using (MySqlConnection myConnection = new MySqlConnection(myConnectionString))
|
|
||||||
|
if (firstAttribute.Parts.Count != 4)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
string title = firstAttribute.Parts[0];
|
||||||
|
string fistName = firstAttribute.Parts[1];
|
||||||
|
string lastName = firstAttribute.Parts[2];
|
||||||
|
|
||||||
|
DateOnly? dateOfBirth =
|
||||||
|
DateOnly.TryParseExact(firstAttribute.Parts[3], Constants.DateOfBirthFormat,
|
||||||
|
out DateOnly date)
|
||||||
|
? date
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (dateOfBirth is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? email = null;
|
||||||
|
Address? address = null;
|
||||||
|
List<PhoneNumber> phoneNumbers = [];
|
||||||
|
|
||||||
|
foreach (CustomerAttributeDto attribute in customer.Attributes.Skip(1))
|
||||||
|
{
|
||||||
|
var attributeParts = attribute.Parts;
|
||||||
|
string identifier = attributeParts[0];
|
||||||
|
|
||||||
|
switch (identifier)
|
||||||
|
{
|
||||||
|
case Constants.AddressAttributeIdentifier:
|
||||||
|
if (attributeParts.Count == 4)
|
||||||
|
{
|
||||||
|
string postalCode = attributeParts[2];
|
||||||
|
|
||||||
|
if (postalCode.Length == 5)
|
||||||
|
{
|
||||||
|
address =
|
||||||
|
new Address(
|
||||||
|
streetAndHouseNumber: attributeParts[1],
|
||||||
|
postalCode: postalCode,
|
||||||
|
city: attributeParts[3]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Constants.EmailAttributeIdentifier:
|
||||||
|
if (attributeParts.Count == 2)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
myConnection.Open();
|
email = attributeParts[1];
|
||||||
|
|
||||||
// Check if user already exists for the customer
|
_ = new MailAddress(
|
||||||
string checkQuery = "SELECT COUNT(*) FROM users WHERE firstname = @firstname AND lastname = @lastname AND customer_id = @customer_id";
|
email); // validate the format of the email address
|
||||||
MySqlCommand checkCmd = new MySqlCommand(checkQuery, myConnection);
|
}
|
||||||
checkCmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
catch (Exception)
|
||||||
checkCmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
|
||||||
checkCmd.Parameters.AddWithValue("@customer_id", customerNumber);
|
|
||||||
|
|
||||||
int userCount = Convert.ToInt32(checkCmd.ExecuteScalar());
|
|
||||||
|
|
||||||
if (userCount > 0)
|
|
||||||
{
|
{
|
||||||
errorLog.Add($"User {user.Firstname} {user.Lastname} already exists for customer {customerNumber}.");
|
return null;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert user
|
|
||||||
string insertQuery = @"
|
|
||||||
INSERT INTO users (gender, firstname, lastname, birthdate, street, postal_code, city, email, customer_id)
|
|
||||||
VALUES (@gender, @firstname, @lastname, @birthdate, @street, @postal_code, @city, @Email, @customer_id)";
|
|
||||||
MySqlCommand insertCmd = new MySqlCommand(insertQuery, myConnection);
|
|
||||||
insertCmd.Parameters.AddWithValue("@gender", user.Gender);
|
|
||||||
insertCmd.Parameters.AddWithValue("@firstname", user.Firstname);
|
|
||||||
insertCmd.Parameters.AddWithValue("@lastname", user.Lastname);
|
|
||||||
insertCmd.Parameters.AddWithValue("@birthdate", user.Birthdate);
|
|
||||||
insertCmd.Parameters.AddWithValue("@street", user.Address.Street);
|
|
||||||
insertCmd.Parameters.AddWithValue("@postal_code", user.Address.PostalCode);
|
|
||||||
insertCmd.Parameters.AddWithValue("@city", user.Address.City);
|
|
||||||
insertCmd.Parameters.AddWithValue("@Email", user.Email);
|
|
||||||
insertCmd.Parameters.AddWithValue("@customer_id", customerNumber);
|
|
||||||
|
|
||||||
insertCmd.ExecuteNonQuery();
|
|
||||||
myConnection.Close();
|
|
||||||
}
|
}
|
||||||
catch (MySqlException ex)
|
else
|
||||||
{
|
{
|
||||||
errorLog.Add($"Database error: {ex.Message}");
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Constants.PhoneNumberAttributeIdentifier:
|
||||||
|
if (attributeParts.Count == 3)
|
||||||
|
{
|
||||||
|
string areaCode = attributeParts[1];
|
||||||
|
string number = attributeParts[2];
|
||||||
|
|
||||||
|
if (areaCode.Length is >= 3 and <= 5
|
||||||
|
&& number.Length is >= 4 and <= 10)
|
||||||
|
{
|
||||||
|
phoneNumbers.Add(new PhoneNumber(areaCode, number));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class User
|
return new Customer(
|
||||||
{
|
title: title,
|
||||||
public string Gender { get; set; }
|
firstName: fistName,
|
||||||
public string Firstname { get; set; }
|
lastName: lastName,
|
||||||
public string Lastname { get; set; }
|
dateOfBirth: dateOfBirth.Value,
|
||||||
public string Birthdate { get; set; }
|
email: email,
|
||||||
public Address Address { get; set; }
|
address: address,
|
||||||
public string Email { get; set; }
|
phoneNumbers: phoneNumbers);
|
||||||
public List<Phone> Phones { get; set; } = new List<Phone>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class Address
|
private static void LogInvalidCustomer(List<string> protocol, CustomerDto customerDto)
|
||||||
{
|
{
|
||||||
public string Street { get; set; }
|
protocol.AddRange(
|
||||||
public string PostalCode { get; set; }
|
customerDto
|
||||||
public string City { get; set; }
|
.Attributes
|
||||||
|
.Select(attribute => string.Join(Constants.Separator, attribute.Parts)));
|
||||||
}
|
}
|
||||||
|
|
||||||
class Phone
|
|
||||||
{
|
|
||||||
public string Prefix { get; set; }
|
|
||||||
public string Number { get; set; }
|
|
||||||
}
|
}
|
@ -14,7 +14,7 @@ using System.Reflection;
|
|||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Projekt_Calcan_Conze")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("Projekt_Calcan_Conze")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5c51f68e6cd6355fd53b485ad50b2778d47b8dc0")]
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9f2e9cbec8646502e9f8d8e82a8d93ceb2077c93")]
|
||||||
[assembly: System.Reflection.AssemblyProductAttribute("Projekt_Calcan_Conze")]
|
[assembly: System.Reflection.AssemblyProductAttribute("Projekt_Calcan_Conze")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("Projekt_Calcan_Conze")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("Projekt_Calcan_Conze")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
@ -1 +1 @@
|
|||||||
eae42f1b5ea27ad223d9cfc838d7801feb8aee718629ae41759404b6e801f0e1
|
b7a6e87d6dec130dcc98889acb8324098d6ecce8b407916d22fbac7ae0858d76
|
||||||
|
Binary file not shown.
@ -66,7 +66,7 @@
|
|||||||
"privateAssets": "all"
|
"privateAssets": "all"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.302/PortableRuntimeIdentifierGraph.json"
|
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.400/PortableRuntimeIdentifierGraph.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\bib\.nuget\packages\</NuGetPackageFolders>
|
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\bib\.nuget\packages\</NuGetPackageFolders>
|
||||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.10.1</NuGetToolVersion>
|
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.11.0</NuGetToolVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||||
<SourceRoot Include="C:\Users\bib\.nuget\packages\" />
|
<SourceRoot Include="C:\Users\bib\.nuget\packages\" />
|
||||||
|
@ -1850,7 +1850,7 @@
|
|||||||
"privateAssets": "all"
|
"privateAssets": "all"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.302/PortableRuntimeIdentifierGraph.json"
|
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.400/PortableRuntimeIdentifierGraph.json"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"version": 2,
|
"version": 2,
|
||||||
"dgSpecHash": "eoIzlhHxBoI=",
|
"dgSpecHash": "3YDIWICGXm0=",
|
||||||
"success": true,
|
"success": true,
|
||||||
"projectFilePath": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze.csproj",
|
"projectFilePath": "C:\\Jan_bib_Module\\PMC\\Projekt\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze\\Projekt_Calcan_Conze.csproj",
|
||||||
"expectedPackageFiles": [
|
"expectedPackageFiles": [
|
||||||
|
Loading…
Reference in New Issue
Block a user