public async Task RentableItem_Create_DoesCreate() { //Arrange RentableItem rentableItem = new RentableItem { Brand = Guid.NewGuid().ToString(), ModelName = Guid.NewGuid().ToString(), VinNumber = Guid.NewGuid().ToString(), SerialNumber = Guid.NewGuid().ToString(), PlateNumber = Guid.NewGuid().ToString(), RegistrationDate = DateTime.Now }; //Act await rentableItem.Create(DbContext); RentableItem dbRentableItem = DbContext.RentableItem.AsNoTracking().First(); List <RentableItem> itemList = DbContext.RentableItem.AsNoTracking().ToList(); List <RentableItemVersion> versionList = DbContext.RentableItemsVersion.AsNoTracking().ToList(); // Assert Assert.NotNull(rentableItem); Assert.AreEqual(1, itemList.Count()); Assert.AreEqual(1, versionList.Count()); Assert.AreEqual(rentableItem.Brand, dbRentableItem.Brand); Assert.AreEqual(rentableItem.ModelName, dbRentableItem.ModelName); Assert.AreEqual(rentableItem.PlateNumber, dbRentableItem.PlateNumber); Assert.AreEqual(rentableItem.VinNumber, dbRentableItem.VinNumber); Assert.AreEqual(rentableItem.SerialNumber, dbRentableItem.SerialNumber); Assert.AreEqual(rentableItem.RegistrationDate.ToString(), dbRentableItem.RegistrationDate.ToString()); Assert.AreEqual(rentableItem.WorkflowState, dbRentableItem.WorkflowState); }
public async Task <OperationResult> Create(RentableItemModel rentableItemPnCreateModel) { try { RentableItem rentableItem = new RentableItem { Brand = rentableItemPnCreateModel.Brand, ModelName = rentableItemPnCreateModel.ModelName, RegistrationDate = rentableItemPnCreateModel.RegistrationDate, VinNumber = rentableItemPnCreateModel.VinNumber, SerialNumber = rentableItemPnCreateModel.SerialNumber, PlateNumber = rentableItemPnCreateModel.PlateNumber, eFormId = rentableItemPnCreateModel.EFormId }; await rentableItem.Create(_dbContext); //_dbContext.RentableItem.Add(rentableItemPn); //_dbContext.SaveChanges(); return(new OperationResult(true, _rentableItemsLocalizationService.GetString("RentableItemCreated", rentableItemPnCreateModel.Brand, rentableItemPnCreateModel.ModelName))); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(true, _rentableItemsLocalizationService.GetString("ErrorWhileCreatingRentableItem"))); } }
public async Task ContractRentableItem_Delete_DoesDelete() { //Arrange RentableItem rentableItem = new RentableItem { Brand = Guid.NewGuid().ToString(), ModelName = Guid.NewGuid().ToString(), VinNumber = Guid.NewGuid().ToString(), SerialNumber = Guid.NewGuid().ToString(), PlateNumber = Guid.NewGuid().ToString(), RegistrationDate = DateTime.Now }; await rentableItem.Create(DbContext); Random rnd = new Random(); DateTime contractEnd = DateTime.Now; DateTime contractStart = DateTime.Now; Contract contract = new Contract { Status = 66, ContractEnd = contractEnd, ContractNr = rnd.Next(1, 255), ContractStart = contractStart, CustomerId = rnd.Next(1, 255) }; await contract.Create(DbContext); ContractRentableItem contractRentableItem = new ContractRentableItem { ContractId = contract.Id, RentableItemId = rentableItem.Id }; await contractRentableItem.Create(DbContext); //Act await contractRentableItem.Delete(DbContext); ContractRentableItem dbContractRentableItem = DbContext.ContractRentableItem.AsNoTracking().First(); List <ContractRentableItem> contractRentableItems = DbContext.ContractRentableItem.AsNoTracking().ToList(); List <ContractRentableItemVersion> versionList = DbContext.ContractRentableItemVersion.AsNoTracking().ToList(); //Assert Assert.NotNull(dbContractRentableItem); Assert.AreEqual(1, contractRentableItems.Count); Assert.AreEqual(2, versionList.Count); Assert.AreEqual(contractRentableItem.ContractId, dbContractRentableItem.ContractId); Assert.AreEqual(contractRentableItem.RentableItemId, dbContractRentableItem.RentableItemId); Assert.AreEqual(Constants.WorkflowStates.Removed, dbContractRentableItem.WorkflowState); }
public async Task <OperationResult> Read(string token) { var pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:Token"); if (pluginConfiguration == null) { return(new OperationResult(false)); } if (token == pluginConfiguration.Value) { UserCredential credential; pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:GmailCredentials"); GoogleClientSecrets secrets = NewtonsoftJsonSerializer.Instance.Deserialize <GoogleClientSecrets>(pluginConfiguration.Value); pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:GmailUserName"); string gmailUserName = pluginConfiguration.Value; pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:MailFrom"); string mailFrom = pluginConfiguration.Value; pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:GmailEmail"); string gmailEmail = pluginConfiguration.Value; pluginConfiguration = await _dbContext.PluginConfigurationValues.SingleOrDefaultAsync(x => x.Name == "RentableItemBaseSettings:SdkeFormId"); int eFormId = int.Parse(pluginConfiguration.Value); try { string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( secrets.Secrets, Scopes, gmailUserName, CancellationToken.None, new FileDataStore(credPath, true)).Result; var service = new GmailService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = ApplicationName }); var emailListRequest = service.Users.Messages.List(gmailEmail); emailListRequest.LabelIds = "INBOX"; emailListRequest.IncludeSpamTrash = false; emailListRequest.Q = $"from:{mailFrom}"; emailListRequest.MaxResults = 1; var emailListResponse = await emailListRequest.ExecuteAsync(); if (emailListResponse != null && emailListResponse.Messages != null) { foreach (var email in emailListResponse.Messages) { var emailInfoRequest = service.Users.Messages.Get(gmailEmail, email.Id); var emailInfoResponse = await emailInfoRequest.ExecuteAsync(); if (emailInfoResponse != null) { IList <MessagePart> parts = emailInfoResponse.Payload.Parts; foreach (var part in parts) { if (!string.IsNullOrEmpty(part.Filename)) { string attId = part.Body.AttachmentId; MessagePartBody attachPart = service.Users.Messages.Attachments .Get(gmailEmail, email.Id, attId).Execute(); string attachData = attachPart.Data.Replace("-", "+"); attachData = attachData.Replace("_", "/"); byte[] data = Convert.FromBase64String(attachData); var bytesAsString = Encoding.Default.GetString(data); string[] rawFile = bytesAsString.Split("\n"); var importList = rawFile.Skip(2); string[] list = importList.ToArray(); var headers = rawFile[0].Split(","); foreach (string s in list) { string[] lSplit = s.Split("\",\""); if (lSplit.Length == 8) { Contract contract = null; Customer customer = null; ContractRentableItem contractRentableItem = null; try { customer = _customerDbContext.Customers.SingleOrDefault(x => x.CompanyName == lSplit[1] || x.ContactPerson == lSplit[1]); if (customer == null) { customer = new Customer { CompanyName = lSplit[1] }; await customer.Create(_customerDbContext); } } catch (Exception e) { Console.WriteLine(e); throw; } RentableItem rentableItem = null; try { rentableItem = _dbContext.RentableItem.SingleOrDefault(x => x.Brand == lSplit[2] && x.ModelName == lSplit[3] && x.RegistrationDate == DateTime.Parse(lSplit[4])); if (rentableItem == null) { rentableItem = new RentableItem { Brand = lSplit[2], ModelName = lSplit[3], RegistrationDate = DateTime.Parse(lSplit[4]), VinNumber = lSplit[5], eFormId = eFormId }; await rentableItem.Create(_dbContext); } } catch (Exception e) { Console.WriteLine(e); throw; } try { contract = _dbContext.Contract.SingleOrDefault(x => x.ContractNr == int.Parse(lSplit[0].Replace("\"", ""))); if (contract == null) { contract = new Contract { ContractNr = int.Parse(lSplit[0].Replace("\"", "")), ContractStart = DateTime.Parse(lSplit[6]), ContractEnd = DateTime.Parse(lSplit[7].Replace("\r", "").Replace("\"", "")), CustomerId = customer.Id, Status = 0 }; await contract.Create(_dbContext); } } catch (Exception e) { Console.WriteLine(e); throw; } try { contractRentableItem = _dbContext.ContractRentableItem.SingleOrDefault(x => x.ContractId == contract.Id && x.RentableItemId == rentableItem.Id); if (contractRentableItem == null) { contractRentableItem = new ContractRentableItem { ContractId = contract.Id, RentableItemId = rentableItem.Id }; await contractRentableItem.Create(_dbContext); } } catch (Exception e) { Console.WriteLine(e); throw; } } } } } } } } } catch (Exception e) { Console.WriteLine(e); throw; } } return(new OperationResult(true)); }
public async Task <OperationResult> Import(ImportModel importModel) { try { JToken rawJson = JToken.Parse(importModel.ImportList); int eFormId = importModel.eFormId; JToken rawHeadersJson = JToken.Parse(importModel.Headers); JToken headers = rawHeadersJson; IEnumerable <JToken> rentableObjects = rawJson.Skip(2); Core core = await _coreHelper.GetCore(); foreach (JToken rentableObject in rentableObjects) { Customer customer = null; bool companyNameExists = int.TryParse(headers[1]["headerValue"].ToString(), out int companyNameColumn); if (companyNameExists) { customer = await FindCustomer(companyNameExists, companyNameColumn, headers, rentableObject); if (customer == null) { CustomerFullModel customerModel = CustomerHelper.ComposeValues(new CustomerFullModel(), headers, rentableObject); customer = new Customer { CompanyName = customerModel.CompanyName }; await customer.Create(_customerDbContext); } } RentableItem rentableItem = null; bool brandExists = int.TryParse(headers[2]["headerValue"].ToString(), out int brandColumn); bool modelExists = int.TryParse(headers[3]["headerValue"].ToString(), out int modelColumn); bool registrationDateExists = int.TryParse(headers[4]["headerValue"].ToString(), out int registrationDateColumn); bool vinNumberExists = int.TryParse(headers[5]["headerValue"].ToString(), out int vinNumberColumn); if (brandExists && modelExists && registrationDateExists && vinNumberExists) { rentableItem = await FindRentableItem(brandExists, brandColumn, modelExists, modelColumn, registrationDateExists, registrationDateColumn, vinNumberExists, vinNumberColumn, headers, rentableObject); if (rentableItem == null) { RentableItemModel rentableItemModel = RentableItemHelper.ComposeValues(new RentableItemModel(), headers, rentableObject); rentableItem = new RentableItem { Brand = rentableItemModel.Brand, ModelName = rentableItemModel.ModelName, RegistrationDate = rentableItemModel.RegistrationDate, VinNumber = rentableItemModel.VinNumber, eFormId = eFormId }; await rentableItem.Create(_dbContext); } } Contract contract = null; bool contractNumberExists = int.TryParse(headers[0]["headerValue"].ToString(), out int contractNumberColumn); bool contractStartExists = int.TryParse(headers[6]["headerValue"].ToString(), out int contractStartColumn); bool contractEndExists = int.TryParse(headers[7]["headerValue"].ToString(), out int contractEndColumn); int customerID = customer.Id; if (contractNumberExists && contractStartExists && contractEndExists && customerID != null) { contract = await FindContract(contractNumberExists, contractNumberColumn, contractStartExists, contractStartColumn, contractEndExists, contractEndColumn, customerID, headers, rentableObject); if (contract == null) { ContractModel contractModel = ContractsHelper.ComposeValues(new ContractModel(), headers, rentableObject); contract = new Contract { CustomerId = customerID, ContractEnd = contractModel.ContractEnd, ContractStart = contractModel.ContractStart, ContractNr = contractModel.ContractNr }; await contract.Create(_dbContext); } } ContractRentableItem contractRentableItem = null; if (contract.Id != null && rentableItem.Id != null) { contractRentableItem = await FindtContractRentableItem(contract.Id, rentableItem.Id); if (contractRentableItem == null) { contractRentableItem = new ContractRentableItem { ContractId = contract.Id, RentableItemId = rentableItem.Id }; await contractRentableItem.Create(_dbContext); } } } return(new OperationResult(true, "Import Successful")); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult(false, "You goofed up")); } }