public static PublisherEntity MockOne() { PublisherEntity entity = new PublisherEntity(); entity.Guid = SecurityUtil.CreateUniqueToken(); entity.Type = PublisherType.Online; entity.Name = "中文名字"; entity.Books = new List <string>() { "Cat", "Dog" }; entity.BookAuthors = new Dictionary <string, Author>() { { "Cat", new Author() { Mobile = "111", Name = "BB" } }, { "Dog", new Author() { Mobile = "222", Name = "sx" } } }; return(entity); }
public void EntityMapper_ToParameter_Test(EngineType engineType) { PublisherEntity publisherEntity = Mocker.MockOnePublisherEntity(); var emit_results = publisherEntity.ToParameters(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1); var reflect_results = publisherEntity.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1); AssertEqual(emit_results, reflect_results, engineType); //PublisherEntity2 PublisherEntity2 publisherEntity2 = new PublisherEntity2(); var emit_results2 = publisherEntity2.ToParameters(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1); var reflect_results2 = publisherEntity2.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1); AssertEqual(emit_results2, reflect_results2, engineType); //PublisherEntity3 PublisherEntity3 publisherEntity3 = new PublisherEntity3(); var emit_results3 = publisherEntity3.ToParameters(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1); var reflect_results3 = publisherEntity3.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1); AssertEqual(emit_results3, reflect_results3, engineType); }
public async Task Test_4_Add_PublisherEntityAsync() { IDatabase database = _mysql; ITransaction transaction = _mysqlTransaction; TransactionContext tContext = await transaction.BeginTransactionAsync <PublisherEntity>().ConfigureAwait(false); try { IList <PublisherEntity> lst = new List <PublisherEntity>(); for (int i = 0; i < 10; ++i) { PublisherEntity entity = Mocker.MockOnePublisherEntity(); await database.AddAsync(entity, "lastUsre", tContext).ConfigureAwait(false); lst.Add(entity); } await transaction.CommitAsync(tContext).ConfigureAwait(false); Assert.True(lst.All(p => p.Id > 0)); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(tContext).ConfigureAwait(false); throw; } }
public async Task <int> FindOrUpsertPublisher(String name) { int retval = 0; using (var dataContext = new AppDbContext()) { var publisher = dataContext.Publishers.FirstOrDefault(x => x.Name == name); if (publisher != null) { retval = publisher.Id; } else { var newPublisher = new PublisherEntity { Name = name }; try { dataContext.Publishers.Add(newPublisher); var success = dataContext.SaveChanges(); retval = newPublisher.Id; } catch (Exception e) { var test = e.Message; } } } return(retval); }
public static PublisherEntity TranslatePublisherContractDataToPublisherEntity(Publisher publisherContract) { var publisherEntity = new PublisherEntity(); publisherEntity.ID = publisherContract.ID; publisherEntity.PublisherName = publisherContract.PublisherName; publisherEntity.Location = publisherContract.Location; return(publisherEntity); }
public int Create(CreatePublisherRequest request) { var publisherEntity = new PublisherEntity() { LicenceNumber = request.LicenceNumber, Name = request.Name }; publisherRepository.Create(publisherEntity); return(publisherEntity.Id); }
public void TranslateBookContractDataToBookEntity(Book bookContract, ref BookEntity bookEntity) { bookEntity.ID = bookContract.ID; bookEntity.Title = bookContract.Title; bookEntity.ISBN = bookContract.ISBN; bookEntity.Pages = bookContract.Pages; bookEntity.Description = bookContract.Description; PublisherEntity newPublisher = this.Context.Publishers.FirstOrDefault(x => bookContract.Publisher.ID == x.ID); bookEntity.Publisher = newPublisher; }
protected override void Seed(AppDbContext context) { PublisherEntity pub1 = new PublisherEntity(); pub1.Id = (context.Publishers.Count() + 1).ToString(); pub1.Title = "Test"; context.Publishers.Add(pub1); context.SaveChanges(); CreateDefaultRoles(context); }
public void Create(PublisherEntity model) { var sql = $"INSERT INTO Publishers (Name, LicenceNumber) OUTPUT INSERTED.Id VALUES ('{model.Name}',{model.LicenceNumber});"; using (SqlConnection connection = new SqlConnection(_connectionString)) { connection.Open(); var insertedId = (int)connection.ExecuteScalar(sql); connection.Close(); model.Id = insertedId; }; }
public async Task Test_2_Batch_Update_PublisherEntityAsync() { IDatabase database = _mysql; ITransaction transaction = _mysqlTransaction; TransactionContext transContext = await transaction.BeginTransactionAsync <PublisherEntity>().ConfigureAwait(false); try { IEnumerable <PublisherEntity> lst = await database.RetrieveAllAsync <PublisherEntity>(transContext).ConfigureAwait(false); for (int i = 0; i < lst.Count(); i += 2) { PublisherEntity entity = lst.ElementAt(i); //entity.Guid = Guid.NewGuid().ToString(); entity.Type = PublisherType.Online; entity.Name = "��sfasfaf������"; entity.Books = new List <string>() { "xxx", "tttt" }; entity.BookAuthors = new Dictionary <string, Author>() { { "Cat", new Author() { Mobile = "111", Name = "BB" } }, { "Dog", new Author() { Mobile = "222", Name = "sx" } } }; } await database.BatchUpdateAsync(lst, "lastUsre", transContext).ConfigureAwait(false); await transaction.CommitAsync(transContext).ConfigureAwait(false); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(transContext).ConfigureAwait(false); throw; } }
public async Task Test_5_Update_PublisherEntityAsync() { IDatabase database = _mysql; ITransaction transaction = _mysqlTransaction; TransactionContext tContext = await transaction.BeginTransactionAsync <PublisherEntity>().ConfigureAwait(false); try { IList <PublisherEntity> testEntities = (await database.PageAsync <PublisherEntity>(1, 1, tContext).ConfigureAwait(false)).ToList(); if (testEntities.Count == 0) { throw new Exception("No Entity to update"); } PublisherEntity entity = testEntities[0]; entity.Books.Add("New Book2"); //entity.BookAuthors.Add("New Book2", new Author() { Mobile = "15190208956", Name = "Yuzhaobai" }); await database.UpdateAsync(entity, "lastUsre", tContext).ConfigureAwait(false); PublisherEntity?stored = await database.ScalarAsync <PublisherEntity>(entity.Id, tContext).ConfigureAwait(false); await transaction.CommitAsync(tContext).ConfigureAwait(false); Assert.True(stored?.Books.Contains("New Book2")); //Assert.True(stored?.BookAuthors["New Book2"].Mobile == "15190208956"); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(tContext).ConfigureAwait(false); throw; } }
public async Task Test_9_UpdateLastTimeTestAsync() { IDatabase database = _mysql; ITransaction transaction = _mysqlTransaction; TransactionContext transactionContext = await transaction.BeginTransactionAsync <PublisherEntity>().ConfigureAwait(false); //TransactionContext? transactionContext = null; try { PublisherEntity item = Mocker.MockOnePublisherEntity(); await database.AddAsync(item, "xx", transactionContext).ConfigureAwait(false); //await database.AddOrUpdateAsync(item, "sfas", transactionContext).ConfigureAwait(false); await database.DeleteAsync(item, "xxx", transactionContext).ConfigureAwait(false); IList <PublisherEntity> testEntities = (await database.PageAsync <PublisherEntity>(1, 1, transactionContext).ConfigureAwait(false)).ToList(); if (testEntities.Count == 0) { throw new Exception("No Entity to update"); } PublisherEntity entity = testEntities[0]; entity.Books.Add("New Book2"); //entity.BookAuthors.Add("New Book2", new Author() { Mobile = "15190208956", Name = "Yuzhaobai" }); await database.UpdateAsync(entity, "lastUsre", transactionContext).ConfigureAwait(false); PublisherEntity?stored = await database.ScalarAsync <PublisherEntity>(entity.Id, transactionContext).ConfigureAwait(false); item = Mocker.MockOnePublisherEntity(); await database.AddAsync(item, "xx", transactionContext).ConfigureAwait(false); var fetched = await database.ScalarAsync <PublisherEntity>(item.Id, transactionContext).ConfigureAwait(false); Assert.Equal(item.LastTime, fetched !.LastTime); fetched.Name = "ssssss"; await database.UpdateAsync(fetched, "xxx", transactionContext).ConfigureAwait(false); await transaction.CommitAsync(transactionContext).ConfigureAwait(false); } catch { await transaction.RollbackAsync(transactionContext).ConfigureAwait(false); throw; } }
public async Task Test_8_LastTimeTestAsync() { IDatabase database = _mysql; PublisherEntity item = Mocker.MockOnePublisherEntity(); await database.AddAsync(item, "xx", null).ConfigureAwait(false); var fetched = await database.ScalarAsync <PublisherEntity>(item.Id, null).ConfigureAwait(false); Assert.Equal(item.LastTime, fetched !.LastTime); fetched.Name = "ssssss"; await database.UpdateAsync(fetched, "xxx", null).ConfigureAwait(false); fetched = await database.ScalarAsync <PublisherEntity>(item.Id, null).ConfigureAwait(false); //await database.AddOrUpdateAsync(item, "ss", null); fetched = await database.ScalarAsync <PublisherEntity>(item.Id, null).ConfigureAwait(false); //Batch var items = Mocker.GetPublishers(); ITransaction transaction = _mysqlTransaction; TransactionContext trans = await transaction.BeginTransactionAsync <PublisherEntity>().ConfigureAwait(false); try { await database.BatchAddAsync(items, "xx", trans).ConfigureAwait(false); var results = await database.RetrieveAsync <PublisherEntity>(item => SqlStatement.In(item.Id, true, items.Select(item => (object)item.Id).ToArray()), trans).ConfigureAwait(false); await database.BatchUpdateAsync(items, "xx", trans).ConfigureAwait(false); var items2 = Mocker.GetPublishers(); await database.BatchAddAsync(items2, "xx", trans).ConfigureAwait(false); results = await database.RetrieveAsync <PublisherEntity>(item => SqlStatement.In(item.Id, true, items2.Select(item => (object)item.Id).ToArray()), trans).ConfigureAwait(false); await database.BatchUpdateAsync(items2, "xx", trans).ConfigureAwait(false); await transaction.CommitAsync(trans).ConfigureAwait(false); } catch { await transaction.RollbackAsync(trans).ConfigureAwait(false); throw; } finally { } }
public Publisher(PublisherEntity dbPublisher) { this.ID = dbPublisher.ID; this.PublisherName = dbPublisher.PublisherName; this.Location = dbPublisher.Location; }
public void Update(PublisherEntity model) { throw new System.NotImplementedException(); }
public ExecutionResult ReadFile(string filePath) { if (!File.Exists(filePath)) { return(BadResult("Ошибка чтения. Такого файла не существует")); } var strings = File.ReadAllLines(filePath).ToList(); int index = default; try { Context.BeginTransaction(); Context.Books.ClearTable(); Context.Users.ClearTable(); Context.Publishers.ClearTable(); Context.Authors.ClearTable(); for (index = 0; index < strings.Count; index++) { var str = strings[index]; var recordFields = str.Split(Constants.Serialization.FileDelimiter); switch (recordFields[0]) { case nameof(AuthorEntity): { if (recordFields.Length != Constants.DataAnnotationConstants.AuthorTableColumnsCount) { return(FileParseBadResult($"Ошибка чтения. Некорректная запись на строке {index + 1}")); } var authorEntity = new AuthorEntity { Id = Convert.ToInt32(recordFields[1]), Name = recordFields[2], Surname = recordFields[3], Patronymic = recordFields[4] }; Context.Authors.SaveEntityWithId(authorEntity); break; } case nameof(PublisherEntity): { if (recordFields.Length != Constants.DataAnnotationConstants.PublisherTableColumnsCount) { return(FileParseBadResult($"Ошибка чтения. Некорректная запись на строке {index + 1}")); } var publisherEntity = new PublisherEntity { Id = Convert.ToInt32(recordFields[1]), Name = recordFields[2] }; Context.Publishers.SaveEntityWithId(publisherEntity); break; } case nameof(UserEntity): { if (recordFields.Length != Constants.DataAnnotationConstants.UserTableColumnsCount) { return(FileParseBadResult($"Ошибка чтения. Некорректная запись на строке {index + 1}")); } var userEntity = new UserEntity { Id = Convert.ToInt32(recordFields[1]), Login = recordFields[2], Password = recordFields[3], Role = EnumExtensions.GetValueFromName <RoleType>(recordFields[4]), LibraryCardNumber = recordFields[5] }; Context.Users.SaveEntityWithId(userEntity); break; } case nameof(BookEntity): { if (recordFields.Length != Constants.DataAnnotationConstants.BookTableColumnsCount) { return(FileParseBadResult($"Ошибка чтения. Некорректная запись на строке {index + 1}")); } var bookEntity = new BookEntity { Id = Convert.ToInt32(recordFields[1]), RegNumber = recordFields[2], Name = recordFields[3], NumberOfPages = string.IsNullOrEmpty(recordFields[4]) ? default : Convert.ToInt32(recordFields[4]), PublicationYear = string.IsNullOrEmpty(recordFields[5]) ? default : Convert.ToInt32(recordFields[5]), IsBookInLibrary = string.IsNullOrEmpty(recordFields[6]) ? default : Convert.ToBoolean(recordFields[6]), PublisherId = string.IsNullOrEmpty(recordFields[7]) ? (int?)null : Convert.ToInt32(recordFields[7]), AuthorId = string.IsNullOrEmpty(recordFields[8]) ? (int?)null : Convert.ToInt32(recordFields[8]), LastUserId = string.IsNullOrEmpty(recordFields[9])? (int?)null : Convert.ToInt32(recordFields[9]) }; Context.Books.SaveEntityWithId(bookEntity); break; } default: { return(FileParseBadResult($"Ошибка чтения. Некорректная запись на строке {index + 1}")); } } } } catch (Exception) { return(FileParseBadResult($"Формат файла нарушен. Ошибка на строке: {index + 1}")); } Context.CommitTransaction(); return(SuccessResult()); }
public string AddPublisher(Publisher publisher) { var existingTopics = _context.Topics.ToList(); var existingAuthors = _context.Authors.ToList(); var publisherEntity = new PublisherEntity { Title = publisher.Title, Description = publisher.Description, MonthlySubscriptionPrice = publisher.MonthlySubscriptionPrice, ImagePath = publisher.ImagePath, IsRemoved = false }; if (_context.Publishers.Count() == 0) { publisherEntity.Id = "1"; } else { publisherEntity.Id = (Convert.ToInt32(_context.Publishers.ToList() .OrderByDescending(p => Convert.ToInt32(p.Id)) .ToList().First().Id) + 1).ToString(); } foreach (var topic in publisher.Topics) { if (existingTopics.Any(t => t.Title == topic.Title)) { publisherEntity.Topics.Add(existingTopics.Where(t => t.Title == topic.Title).First()); } else { var topicEntity = new TopicEntity { Title = topic.Title }; if (_context.Topics.Count() == 0) { topicEntity.Id = "1"; } else { topicEntity.Id = (Convert.ToInt32(_context.Topics.ToList() .OrderByDescending(t => Convert.ToInt32(t.Id)) .ToList().First().Id) + 1).ToString(); } publisherEntity.Topics.Add(topicEntity); _context.Topics.Add(topicEntity); } _context.SaveChanges(); } foreach (var author in publisher.Authors) { if (existingAuthors.Any(a => a.Name == author.Name)) { publisherEntity.Authors.Add(existingAuthors.Where(a => a.Name == author.Name).First()); } else { var authorEntity = new AuthorEntity { Name = author.Name }; if (_context.Authors.Count() == 0) { authorEntity.Id = "1"; } else { authorEntity.Id = (Convert.ToInt32(_context.Authors.ToList() .OrderByDescending(a => Convert.ToInt32(a.Id)) .ToList().First().Id) + 1).ToString(); } publisherEntity.Authors.Add(authorEntity); _context.Authors.Add(authorEntity); } _context.SaveChanges(); } _context.Publishers.Add(publisherEntity); _context.SaveChanges(); return(publisherEntity.Id); }