public async Task RateBarAsync(int userId, int userRating, int barId) { if (await dbContext.BarRating.AnyAsync(p => (p.UserId == userId && p.BarId == barId))) { var givenRating = await dbContext.BarRating.FirstOrDefaultAsync(p => (p.UserId == userId && p.BarId == barId)); givenRating.Rating = userRating; await dbContext.SaveChangesAsync(); await barService.UpdateAverageRatingAsync(barId); } else { var user = await FindUserByIdAsync(userId); var bar = await barService.FindBarByIdAsync(barId); var barRating = new BarRating() { User = user, Bar = bar, Rating = userRating, BarName = bar.Name, UserName = user.UserName }; await dbContext.BarRating.AddAsync(barRating); await dbContext.SaveChangesAsync(); await barService.UpdateAverageRatingAsync(barId); } }
public async Task <BarRatingDto> CreateAsync(BarRatingDto tempBarRating) { if (tempBarRating == null) { throw new BusinessLogicException(ExceptionMessages.BarRatingNull); } var barRating = new BarRating { Value = tempBarRating.Value, UserId = tempBarRating.UserId, BarId = tempBarRating.BarId, CreatedOn = tempBarRating.CreatedOn, ModifiedOn = tempBarRating.ModifiedOn, DeletedOn = tempBarRating.DeletedOn, IsDeleted = tempBarRating.IsDeleted }; await this.context.BarRatings.AddAsync(barRating); await this.context.SaveChangesAsync(); var barRatingDto = this.dtoMapper.MapFrom(barRating); return(barRatingDto); }
public void MapFrom_Should_ReturnCorrectInstanceOf_BarRatingDto() { //Arrange var sut = new BarRatingDtoMapper(); var barRating = new BarRating { Bar = new Bar { Id = Guid.NewGuid(), Name = "testBar", Info = "testInfo", ImagePath = "testPath", Address = "testAddress", GoogleMapsURL = "GoogleMapsURL", Phone = "111-333-666" }, User = new User { Id = Guid.NewGuid(), UserName = "******", Email = "*****@*****.**" }, Value = 5, CreatedOn = DateTime.MinValue, }; //Act var result = sut.MapFrom(barRating); //Assert Assert.IsInstanceOfType(result, typeof(BarRatingDto)); }
public void ReturnCorrectInstance_OfType_BarRatingDto() { //Arrange var sut = new BarRatingDtoMapper(); var bar = new Bar { Id = 1, Name = "TestBar", Info = "TestInfo", }; var user = new User { Id = 1, UserName = "******", Email = "*****@*****.**", }; var barRating = new BarRating { BarId = 1, UserId = 1, Value = 5, CreatedOn = DateTime.MinValue, }; //Act var result = sut.MapDto(barRating); //Assert Assert.IsInstanceOfType(result, typeof(BarRatingDto)); Assert.AreEqual(result.BarId, barRating.BarId); Assert.AreEqual(result.UserId, barRating.UserId); Assert.AreEqual(result.Value, barRating.Value); }
public void BarRating_AddRating_ToNullBarList() { // Arrange var rating = new BarRating(); // Act && Assert Assert.Throws <ArgumentNullException>(() => rating.AddRating(null, 5)); }
public void BarRating_AddEmptyBarList() { // Arrange BarRating rating = new BarRating(); // Act rating.AddBars(null); // Assert }
public void BarRating_AddRatingToNullBarList() { // Arrange BarRating rating = new BarRating(); // Act rating.AddRating(null, 100); // Assert }
public void BarRating_Sort_NormalEnum([Range(0, 3, 1)] CompareType compareType) { // Arrange var rating = new BarRating(); // Act rating.Sort(compareType); // Assert, should be no exceptions. //Not sure if should test if sorting was successful. Maybe it's covered in Comparers unit testing? }
public void BarRating_SortUnknownEnum() { // Arrange BarRating rating = new BarRating(); // Act rating.Sort((CompareType)99999); // Assert }
public async Task Return_Correct_Model_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(Return_Correct_Model_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarRatingDtoMapper = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var user = new User { Id = 1 }; var bar = new Bar { Id = 1 }; var rating = new BarRating { Id = 1, UserId = 1, BarId = 1, Value = 5, }; var ratingDto = new BarRatingDto { Id = 1, UserId = 1, BarId = 1, Value = 5, }; mockBarRatingDtoMapper.Setup(x => x.MapDto(It.IsAny <BarRating>())).Returns(ratingDto); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.BarRatings.AddAsync(rating); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarRatingService(assertContext, mockBarRatingDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetRatingAsync(1, 1); Assert.IsInstanceOfType(result, typeof(BarRatingDto)); Assert.AreEqual(1, result.Id); Assert.AreEqual(1, result.UserId); Assert.AreEqual(1, result.BarId); Assert.AreEqual(5, result.Value); } }
public async Task CorrectlyCreateBarRating() { //Arrange var options = TestUtilities.GetOptions(nameof(CorrectlyCreateBarRating)); var mapperMock = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var barId = Guid.NewGuid(); var barIdTwo = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var createdOn = DateTime.UtcNow; var entityDto = new BarRatingDto { BarId = barId, UserId = userId, UserName = "******", Value = 2, CreatedOn = createdOn }; var entity = new BarRating { BarId = barIdTwo, UserId = userIdTwo, Value = 2, CreatedOn = createdOn }; mapperMock.Setup(x => x.MapFrom(It.IsAny <BarRating>())).Returns(entityDto); using (var assertContext = new CWContext(options)) { //Assert var sut = new BarRatingService(assertContext, mapperMock.Object); var result = await sut.CreateAsync(entityDto); Assert.IsInstanceOfType(result, typeof(BarRatingDto)); Assert.AreEqual(barId, result.BarId); Assert.AreEqual(userId, result.UserId); Assert.AreEqual(userId, result.UserId); Assert.AreEqual(2, result.Value); Assert.AreEqual("testusername", result.UserName); Assert.AreEqual(entityDto.BarId, result.BarId); Assert.AreEqual(entityDto.UserId, result.UserId); Assert.AreEqual(entityDto.UserId, result.UserId); Assert.AreEqual(entityDto.Value, result.Value); Assert.AreEqual(entityDto.UserName, result.UserName); } }
public void ReturnCorrectValue_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectValue_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarRatingDtoMapper = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var user1 = new User { Id = 1 }; var user2 = new User { Id = 2 }; var bar = new Bar { Id = 1 }; var rating1 = new BarRating { Id = 1, UserId = 1, BarId = 1, Value = 5, }; var rating2 = new BarRating { Id = 2, UserId = 2, BarId = 1, Value = 3, }; using (var arrangeContext = new CocktailMagicianContext(options)) { arrangeContext.Bars.AddAsync(bar); arrangeContext.Users.AddAsync(user1); arrangeContext.Users.AddAsync(user2); arrangeContext.BarRatings.AddAsync(rating1); arrangeContext.BarRatings.AddAsync(rating2); arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarRatingService(assertContext, mockBarRatingDtoMapper.Object, mockDateTimeProvider.Object); var result = sut.GetAverageBarRating(1); Assert.AreEqual(4, result); } }
void BarRatingEventModule_After(BarRating sender, CommonEventArgs eventArgs) { ActivityService activityService = new ActivityService(); if (eventArgs.EventOperationType == EventOperationType.Instance().Create()) { Activity actvity = Activity.New(); actvity.ActivityItemKey = ActivityItemKeys.Instance().CreateBarRating(); actvity.ApplicationId = BarConfig.Instance().ApplicationId; BarThreadService barThreadService = new BarThreadService(); BarThread barThread = barThreadService.Get(sender.ThreadId); if (barThread == null) { return; } var barUrlGetter = BarUrlGetterFactory.Get(barThread.TenantTypeId); if (barUrlGetter == null) { return; } actvity.IsOriginalThread = true; actvity.IsPrivate = barUrlGetter.IsPrivate(barThread.SectionId); actvity.OwnerId = barThread.SectionId; actvity.OwnerName = barThread.BarSection.Name; actvity.OwnerType = barUrlGetter.ActivityOwnerType; actvity.ReferenceId = barThread.ThreadId; actvity.ReferenceTenantTypeId = TenantTypeIds.Instance().BarThread(); actvity.SourceId = sender.RatingId; actvity.TenantTypeId = TenantTypeIds.Instance().BarRating(); actvity.UserId = sender.UserId; //自己回复自己时,不向自己的动态收件箱推送动态 if (actvity.UserId == barThread.UserId) { activityService.Generate(actvity, false); } else { activityService.Generate(actvity, true); } } else { activityService.DeleteSource(TenantTypeIds.Instance().BarRating(), sender.RatingId); } }
public async Task <bool> CreateBarRatingAsync(BarRatingCreate model) { var entity = new BarRating { Comment = model.Comment, BarId = model.BarId, Grade = model.Grade, UserId = _userId, StayDate = model.StayDate }; _context.Ratings.Add(entity); var changeCount = await _context.SaveChangesAsync(); return(changeCount == 1); }
public static BarRatingDTO GetDTO(this BarRating rating) { if (rating == null) { throw new ArgumentNullException(); } return(new BarRatingDTO { Bar = rating.Bar, BarId = rating.BarId, User = rating.User, UserId = rating.UserId, Rating = rating.Rating, }); }
public async Task Throw_WhenNoBarRatingDtoFound() { //Arrange var options = TestUtilities.GetOptions(nameof(Throw_WhenNoBarRatingDtoFound)); var mapperMock = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var barId = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var entity = new BarRating { BarId = barId, UserId = userId, Value = 2 }; var entityDto = new BarRatingDto { BarId = barId, UserId = userId, Value = 2 }; mapperMock.Setup(x => x.MapFrom(It.IsAny <BarRating>())).Returns(entityDto); using (var arrangeContext = new CWContext(options)) { await arrangeContext.BarRatings.AddAsync(entity); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Act & Assert var sut = new BarRatingService(assertContext, mapperMock.Object); await Assert.ThrowsExceptionAsync <BusinessLogicException>(() => sut.GetRatingAsync(barId, userId)); } }
public ActionResult _CreateBarRating(long ActivityId) { Activity activity = activityService.Get(ActivityId); if (activity == null) { return(HttpNotFound()); } BarRating rating = barRatingService.Get(activity.SourceId); if (rating == null) { return(HttpNotFound()); } ViewData["BarRating"] = rating; BarThread thread = barThreadService.Get(activity.ReferenceId); if (thread == null) { return(HttpNotFound()); } ViewData["ActivityId"] = ActivityId; ViewData["Attachments"] = thread.Attachments.Where(n => n.MediaType == MediaType.Image).FirstOrDefault(); if (thread.BarSection.TenantTypeId != TenantTypeIds.Instance().Bar()) { var tenantType = new TenantTypeService().Get(thread.BarSection.TenantTypeId); string tenantTypeName = string.Empty; if (tenantType != null) { tenantTypeName = tenantType.Name; } ViewData["tenantTypeName"] = tenantTypeName; } return(View(thread)); }
public async Task <BarRatingDto> CreateRatingAsync(BarRatingDto tempBarRating) { if (tempBarRating == null) { return(null); } if (tempBarRating.Value == 0 || tempBarRating.UserId < 1 || tempBarRating.BarId < 1) { return(null); } var rating = await this.GetRatingAsync(tempBarRating.BarId, tempBarRating.UserId); if (rating == null) { var newBarRating = new BarRating { Id = tempBarRating.Id, Value = tempBarRating.Value, BarId = tempBarRating.BarId, UserId = tempBarRating.UserId, CreatedOn = this.dateTimeProvider.GetDateTime(), }; await this.context.AddAsync(newBarRating); await this.context.SaveChangesAsync(); var barRatingDto = this.dtoMapper.MapDto(newBarRating); return(barRatingDto); } var editdRating = await this.EditRatingAsync(tempBarRating.BarId, tempBarRating.UserId, tempBarRating.Value); return(editdRating); }
public void MapFrom_Should_CorrectlyMapFrom_BarRating_To_BarRatingDto() { //Arrange var sut = new BarRatingDtoMapper(); var barRating = new BarRating { Bar = new Bar { Id = Guid.NewGuid(), Name = "testBar", Info = "testInfo", ImagePath = "testPath", Address = "testAddress", GoogleMapsURL = "GoogleMapsURL", Phone = "111-333-666" }, User = new User { Id = Guid.NewGuid(), UserName = "******", Email = "*****@*****.**" }, Value = 5, CreatedOn = DateTime.MinValue, }; //Act var result = sut.MapFrom(barRating); //Assert Assert.AreEqual(result.BarId, barRating.BarId); Assert.AreEqual(result.UserId, barRating.UserId); Assert.AreEqual(result.Value, barRating.Value); Assert.AreEqual(result.CreatedOn, barRating.CreatedOn); }
public async Task GetAllRatings_ReturnInstanceOfCollectionBarRatingDtos() { //Arrange var options = TestUtilities.GetOptions(nameof(GetAllRatings_ReturnInstanceOfCollectionBarRatingDtos)); var mapperMock = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var barId = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var bar = new Bar { Id = barId, Name = "testName", }; var user = new User { Id = userId, UserName = "******" }; var entity = new BarRating { BarId = barId, UserId = userId, Value = 2, CreatedOn = DateTime.UtcNow, }; var entityTwo = new BarRating { BarId = barId, UserId = userIdTwo, Value = 2, CreatedOn = DateTime.UtcNow, }; var list = new List <BarRatingDto>() { new BarRatingDto { BarId = barId, UserId = userId, Value = 2 }, new BarRatingDto { BarId = barId, UserId = userIdTwo, Value = 2 }, }; mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <BarRating> >())).Returns(list); using (var arrangeContext = new CWContext(options)) { //await arrangeContext.Bars.AddAsync(bar); //await arrangeContext.Users.AddAsync(user); await arrangeContext.BarRatings.AddAsync(entity); await arrangeContext.BarRatings.AddAsync(entityTwo); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Act & Assert var sut = new BarRatingService(assertContext, mapperMock.Object); var result = await sut.GetAllRatingsAsync(barId); Assert.IsInstanceOfType(result, typeof(ICollection <BarRatingDto>)); Assert.AreEqual(2, result.Count()); Assert.AreEqual(entity.Value, result.First().Value); Assert.AreEqual(entity.UserId, result.First().UserId); Assert.AreEqual(entityTwo.Value, result.Last().Value); Assert.AreEqual(entityTwo.UserId, result.Last().UserId); } }
public async Task ReturnCorrectModels_When_ParamsAreValid() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectModels_When_ParamsAreValid)); var mockDateTimeProvider = new Mock <IDateTimeProvider>(); var mockBarRatingDtoMapper = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var user1 = new User { Id = 1 }; var user2 = new User { Id = 2 }; var bar = new Bar { Id = 1 }; var rating1 = new BarRating { Id = 1, UserId = 1, BarId = 1, Value = 5, }; var rating2 = new BarRating { Id = 2, UserId = 2, BarId = 1, Value = 3, }; var list = new List <BarRatingDto>() { new BarRatingDto { Id = 1, UserId = 1, BarId = 1, Value = 5, }, new BarRatingDto { Id = 2, UserId = 2, BarId = 1, Value = 3, } }; mockBarRatingDtoMapper.Setup(x => x.MapDto(It.IsAny <ICollection <BarRating> >())).Returns(list); using (var arrangeContext = new CocktailMagicianContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user1); await arrangeContext.Users.AddAsync(user2); await arrangeContext.BarRatings.AddAsync(rating1); await arrangeContext.BarRatings.AddAsync(rating2); await arrangeContext.SaveChangesAsync(); } //Act and Assert using (var assertContext = new CocktailMagicianContext(options)) { var sut = new BarRatingService(assertContext, mockBarRatingDtoMapper.Object, mockDateTimeProvider.Object); var result = await sut.GetAllBarRatingAsync(1); Assert.AreEqual(2, result.Count); Assert.AreEqual(1, result.First().Id); Assert.AreEqual(1, result.First().UserId); Assert.AreEqual(1, result.First().BarId); Assert.AreEqual(5, result.First().Value); Assert.AreEqual(2, result.Last().Id); Assert.AreEqual(2, result.Last().UserId); Assert.AreEqual(1, result.Last().BarId); Assert.AreEqual(3, result.Last().Value); } }
public async Task ReturnCorrectElementsWhen_SortIsByRatingAsc() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectElementsWhen_SortIsByRatingAsc)); var mapperMock = new Mock <IDtoMapper <Bar, BarDto> >(); var searchMapperMock = new Mock <IDtoMapper <Bar, SearchBarDto> >(); var cocktailMapperMock = new Mock <IDtoMapper <Cocktail, CocktailDto> >(); var testGuid = Guid.NewGuid(); var testGuid2 = Guid.NewGuid(); var testGuid3 = Guid.NewGuid(); var bar1 = new Bar { Id = testGuid, Name = "atestBar", }; var bar2 = new Bar { Id = testGuid2, Name = "ztestBar", }; var user = new User { Id = testGuid3, UserName = "******" }; var barRating = new BarRating { BarId = bar1.Id, Bar = bar1, User = user, UserId = user.Id, Value = 5 }; user.BarRatings.Add(barRating); bar1.Ratings.Add(barRating); var list = new List <BarDto>() { new BarDto { Id = testGuid2, Name = "ztestBar" }, new BarDto { Id = testGuid, Name = "atestBar" } }; mapperMock.Setup(x => x.MapFrom(It.IsAny <ICollection <Bar> >())).Returns(list); using (var arrangeContext = new CWContext(options)) { await arrangeContext.Bars.AddAsync(bar1); await arrangeContext.Bars.AddAsync(bar2); await arrangeContext.Users.AddAsync(user); await arrangeContext.BarRatings.AddAsync(barRating); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Act & Assert var sut = new BarService(assertContext, mapperMock.Object, searchMapperMock.Object, cocktailMapperMock.Object); var result = await sut.GetFiveBarsAsync(1, "Rating"); Assert.AreEqual("ztestBar", result.ToArray()[0].Name); Assert.AreEqual(testGuid2, result.ToArray()[0].Id); Assert.AreEqual("atestBar", result.ToArray()[1].Name); Assert.AreEqual(testGuid, result.ToArray()[1].Id); } }
public async Task ReturnCorrectInstanceOfBarRatingDto() { //Arrange var options = TestUtilities.GetOptions(nameof(ReturnCorrectInstanceOfBarRatingDto)); var mapperMock = new Mock <IDtoMapper <BarRating, BarRatingDto> >(); var barId = Guid.NewGuid(); var userId = Guid.NewGuid(); var userIdTwo = Guid.NewGuid(); var bar = new Bar { Name = "BarTest", Id = barId, }; var user = new User { UserName = "******", Id = userId, }; var entity = new BarRating { BarId = bar.Id, UserId = user.Id, Value = 2 }; var entityDto = new BarRatingDto { BarId = bar.Id, UserId = user.Id, Value = 2 }; mapperMock.Setup(x => x.MapFrom(It.IsAny <BarRating>())).Returns(entityDto); using (var arrangeContext = new CWContext(options)) { await arrangeContext.Bars.AddAsync(bar); await arrangeContext.Users.AddAsync(user); await arrangeContext.BarRatings.AddAsync(entity); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new CWContext(options)) { //Act & Assert var sut = new BarRatingService(assertContext, mapperMock.Object); var result = await sut.GetRatingAsync(barId, userId); Assert.IsInstanceOfType(result, typeof(BarRatingDto)); Assert.AreEqual(entity.Value, result.Value); Assert.AreEqual(entity.UserId, result.UserId); } }