private async Task CreateCustomer(Customer customer) { var sha256 = SHA256.Create(); var valueToHash = string.Concat(customer.Firstname, customer.Lastname, customer.Email); var id = ""; try { while (true) { id = encryptService.GenerateHash(sha256, valueToHash); // Check if the ID is already assigned var customerResponse = await this.customerContainer.ReadItemAsync <Customer>(id, new PartitionKey(customer.Country)); Console.WriteLine("Customer with id: {0} already exists\n", customerResponse.Resource.Id); valueToHash = id; } } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { customer.Id = id; // Create the customer var customerResponse = await this.customerContainer.CreateItemAsync <Customer>(customer, new PartitionKey(customer.Country)); Console.WriteLine("Customer successfully created with id: {0}\n", customerResponse.Resource.Id); } }
private async Task CreateReservation(Reservation reservation) { var sha256 = SHA256.Create(); var valueToHash = string.Concat(reservation.CustomerId, reservation.ApartmentId, reservation.Of, reservation.To); var id = ""; try { while (true) { id = encryptService.GenerateHash(sha256, valueToHash); // Check if the ID is already assigned var reservationResponse = await this.reservationContainer.ReadItemAsync <Reservation>(id, new PartitionKey(reservation.Type)); Console.WriteLine("Reservation with id: {0} already exists\n", reservationResponse.Resource.Id); valueToHash = id; } } catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound) { reservation.Id = id; // Create the reservation var reservationResponse = await this.reservationContainer.CreateItemAsync <Reservation>(reservation, new PartitionKey(reservation.Type)); Console.WriteLine("Reservation successfully created with id: {0}\n", reservationResponse.Resource.Id); } }