public void Add(Brewery brewery) { using (var context = new MicrobrewitContext()) { brewery.Origin = null; brewery.CreatedDate = DateTime.Now; brewery.UpdatedDate = DateTime.Now; context.Entry(brewery).State = EntityState.Added; try { context.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); Log.DebugFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } } }
public virtual void Update(Brewery brewery) { using (var context = new MicrobrewitContext()) { brewery.UpdatedDate = DateTime.Now; var originalBrewery = context.Breweries.Include(b => b.Members).SingleOrDefault(b => b.BreweryId == brewery.BreweryId); if (originalBrewery == null) return; brewery.CreatedDate = originalBrewery.CreatedDate; SetChanges(context, originalBrewery, brewery); foreach (var member in brewery.Members) { var existingMember = originalBrewery.Members.Any(m => m.MemberUsername.Equals(member.MemberUsername)); if (existingMember) { var originalMember = originalBrewery.Members.SingleOrDefault(m => m.MemberUsername.Equals(member.MemberUsername)); SetChanges(context, originalMember, member); } else { context.BreweryMembers.Add(member); } } foreach (var brewerySocial in brewery.Socials) { var existingBrewerySocial = context.BrewerySocials.SingleOrDefault( s => s.BreweryId == brewerySocial.BreweryId && s.SocialId == brewerySocial.SocialId); if (existingBrewerySocial != null) { SetChanges(context, existingBrewerySocial, brewerySocial); } else { context.BrewerySocials.Add(brewerySocial); } } brewery.Origin = null; try { context.SaveChanges(); } catch (Exception e) { throw; } } }
public async Task AddAsync_Get_Added() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; await _breweryRepository.AddAsync(newBrewery); var breweries = await _breweryRepository.GetAllAsync(0,20); Assert.True(breweries.Any(b => b.Name == newBrewery.Name)); }
public void Update_Members_One_Gets_Removed() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; newBrewery.Members = new List<BreweryMember> { new BreweryMember {MemberUsername = "******"}, new BreweryMember {MemberUsername = "******"} }; _breweryRepository.Add(newBrewery); var brewery = _breweryRepository.GetSingle(newBrewery.BreweryId); brewery.Members.Remove(brewery.Members.FirstOrDefault()); _breweryRepository.Update(brewery); var updated = _breweryRepository.GetSingle(brewery.BreweryId); Assert.AreEqual(1, updated.Members.Count); }
public void Update_BreweryMember_Gets_Added() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; _breweryRepository.Add(newBrewery); var brewery = _breweryRepository.GetSingle(newBrewery.BreweryId); var breweryMember = new BreweryMember { MemberUsername = "******"}; brewery.Members.Add(breweryMember); _breweryRepository.Update(brewery); var updated = _breweryRepository.GetSingle(brewery.BreweryId); Assert.True(updated.Members.Any(s => s.MemberUsername == brewery.Members.FirstOrDefault().MemberUsername && s.BreweryId == brewery.Members.FirstOrDefault().BreweryId)); }
public void Update_Social_One_Gets_Removed() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; newBrewery.Socials = new List<BrewerySocial> { new BrewerySocial {Site="Homepage",Url = "http://test.com/" + newBrewery.Name}, new BrewerySocial {Site="Twitter",Url = "@" + newBrewery.Name} }; _breweryRepository.Add(newBrewery); var brewery = _breweryRepository.GetSingle(newBrewery.BreweryId); brewery.Socials.Remove(brewery.Socials.FirstOrDefault()); _breweryRepository.Update(brewery); var updated = _breweryRepository.GetSingle(brewery.BreweryId); Assert.AreEqual(1, updated.Socials.Count); }
public async Task AddAsync_BreweryMembers() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; var breweryMembers = new List<BreweryMember> { new BreweryMember{MemberUsername = "******",Role = "Member"}, new BreweryMember{MemberUsername = "******",Role = "Member"}, }; newBrewery.Members = breweryMembers; await _breweryRepository.AddAsync(newBrewery); var brewery = await _breweryRepository.GetSingleAsync(newBrewery.BreweryId); Assert.True(brewery.Members.Any()); }
private async Task UpdateBreweryMembersAsync(DbConnection context, DbTransaction transaction, Brewery brewery) { var breweryMembers = await context.QueryAsync<BreweryMember>( "SELECT * FROM BreweryMembers WHERE BreweryId = @BreweryId", new { brewery.BreweryId }, transaction); await context.ExecuteAsync("DELETE FROM BreweryMembers WHERE BreweryId = @BreweryId and MemberUsername = @MemberUsername;", breweryMembers.Where(bm => brewery.Members.All(m => bm.MemberUsername != m.MemberUsername)).Select(bm => new { brewery.BreweryId, bm.MemberUsername }), transaction); await context.ExecuteAsync( "UPDATE BreweryMembers set Role = @Role, Confirmed = @Confirmed WHERE BreweryId = @BreweryId and MemberUsername = @MemberUsername;", brewery.Members.Where(m => breweryMembers.Any(bm => m.MemberUsername == bm.MemberUsername)), transaction); await context.ExecuteAsync("INSERT BreweryMembers(BreweryId,MemberUsername,Role,Confirmed) VALUES(@BreweryId,@MemberUsername,@Role,@Confirmed);", brewery.Members.Where(m => breweryMembers.All(bm => m.MemberUsername != bm.MemberUsername)).Select(bm => new { brewery.BreweryId, bm.MemberUsername,bm.Role,bm.Confirmed }), transaction); }
private async Task UpdateBrewerySocialsAsync(DbConnection context, DbTransaction transaction, Brewery brewery) { var brewerySocials = await context.QueryAsync<BrewerySocial>("SELECT * FROM BrewerySocials WHERE BreweryId = @BreweryId", new { brewery.BreweryId }, transaction); await context.ExecuteAsync("DELETE FROM BrewerySocials WHERE BreweryId = @BreweryId and SocialId = @SocialId;", brewerySocials.Where( u => brewery.Socials.All(s => u.SocialId != s.SocialId)), transaction); await context.ExecuteAsync( "UPDATE BrewerySocials set Site = @Site, Url = @Url WHERE BreweryId = @BreweryId and SocialId = @SocialId;", brewery.Socials, transaction); await context.ExecuteAsync("INSERT BrewerySocials(BreweryId,Site,Url) VALUES(@BreweryId,@Site,@Url);", brewery.Socials.Where( s => brewerySocials.All(u => u.SocialId != s.SocialId)).Select(s => new { brewery.BreweryId, s.Site, s.Url }), transaction); }
public virtual async Task AddAsync(Brewery brewery) { using (var context = new MicrobrewitContext()) { context.Entry(brewery).State = EntityState.Added; try { await context.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { //foreach (var validationErrors in dbEx.EntityValidationErrors) //{ // foreach (var validationError in validationErrors.ValidationErrors) // { // Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); // Log.DebugFormat("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); // throw dbEx; // } //} throw; } catch (Exception ex) { throw; } } }
public async Task<int> UpdateAsync(Brewery brewery) { using (var context = DapperHelper.GetOpenConnection()) { using (var transaction = context.BeginTransaction()) { try { var result = await context.ExecuteAsync( "UPDATE Breweries set Name = @Name,Description = @Name, Type = @Type, CreatedDate = @CreatedDate, UpdatedDate = @UpdatedDate," + "Longitude = @Longitude, Latitude = @Latitude, Website = @Website, Established = @Established, HeaderImage = @HeaderImage, Avatar = @Avatar," + "OriginId = @OriginId, Address = @Address WHERE BreweryId = @BreweryId;", brewery, transaction); await UpdateBrewerySocialsAsync(context, transaction, brewery); await UpdateBreweryMembersAsync(context, transaction, brewery); transaction.Commit(); return result; } catch (Exception e) { Log.Error(e.ToString()); transaction.Rollback(); throw; } } } }
public async Task AddAsync(Brewery brewery) { using (var context = DapperHelper.GetOpenConnection()) { using (var transaction = context.BeginTransaction()) { try { brewery.UpdatedDate = DateTime.Now; brewery.CreatedDate = DateTime.Now; var breweryId = await context.QueryAsync<int>( "INSERT Breweries(Name,Description,Type,CreatedDate,UpdatedDate,Longitude,Latitude,Website,Established,HeaderImage,Avatar,OriginId,Address) " + "VALUES (@Name,@Description,@Type,@CreatedDate,@UpdatedDate,@Longitude,@Latitude,@Website,@Established,@HeaderImage,@Avatar,@OriginId,@Address); " + "SELECT CAST(SCOPE_IDENTITY() as int);", brewery, transaction); brewery.BreweryId = breweryId.SingleOrDefault(); if (brewery.Socials != null) { await context.ExecuteAsync( "INSERT BrewerySocials(BreweryId,Site,Url) VALUES(@BreweryId,@Site,@Url);", brewery.Socials.Select(u => new { brewery.BreweryId, u.Site, u.Url }), transaction); } if (brewery.Members != null) { await context.ExecuteAsync( "INSERT BreweryMembers(BreweryId,MemberUsername,Role) VALUES(@BreweryId,@MemberUsername,@Role);", brewery.Members.Select(u => new { brewery.BreweryId, u.MemberUsername, u.Role }), transaction); } if (brewery.Beers != null) { await context.ExecuteAsync( "INSERT BreweryBeers(BeerId,BreweryId) VALUES(@BeerId,@BreweryId);", brewery.Beers.Select(b => new { b.BeerId, brewery.BreweryId }), transaction); } transaction.Commit(); } catch (Exception e) { Log.Error(e.ToString()); transaction.Commit(); throw; } } } }
public void Remove(Brewery brewery) { throw new NotImplementedException(); }
public virtual async Task RemoveAsync(Brewery brewery) { using (var context = new MicrobrewitContext()) { context.Entry(brewery).State = EntityState.Deleted; try { await context.SaveChangesAsync(); } catch (Exception e) { Log.Debug(e); throw; } } }
public Task RemoveAsync(Brewery brewery) { throw new NotImplementedException(); }
public void Add_Brewery_Socials() { var newBrewery = new Brewery { Name = "NewBrewery" + DateTime.Now.Ticks, Description = "Somehting", Type = "HomeBrewery", OriginId = 5, Address = "Something", Latitude = 60.3894, Longitude = 5.33, Website = "Something", HeaderImage = "Something", Avatar = "Something", }; var brewerySocials = new List<BrewerySocial> { new BrewerySocial{Site = "Twitter", Url = "@" + newBrewery.Name}, new BrewerySocial{Site = "Fanpage", Url = "http://fanpage.com/" + newBrewery.Name}, }; newBrewery.Socials = brewerySocials; _breweryRepository.Add(newBrewery); var brewery = _breweryRepository.GetSingle(newBrewery.BreweryId); Assert.True(brewery.Socials.Any()); }
public virtual void Remove(Brewery brewery) { using (var context = new MicrobrewitContext()) { context.Entry(brewery).State = EntityState.Deleted; context.SaveChanges(); } }