public Result <NotificationServiceModel> Add(NotificationServiceModel notificationModel, int userId, int groupId) { var result = new Result <NotificationServiceModel>(); try { var newNotification = new NotificationServiceModel() { Description = notificationModel.Description, Type = notificationModel.Type, UserId = userId, GroupId = groupId, CreatedOn = Application.CurrentDate, CreatedBy = userId }; var notificationEntity = _mapper.Map <Notification>(newNotification); _unitOfWork.NotificationRepository.Add(notificationEntity); var notificationServiceModel = _mapper.Map <NotificationServiceModel>(notificationEntity); result.IsSuccess = true; result.Data = notificationServiceModel; return(result); } catch (Exception ex) { _logger.LogCritical(ex.InnerException.Message); result.IsSuccess = false; result.ErrorMessage = ErrorMessage.Add; return(result); } }
public async Task AddNotificationToUserAsync_ShouldAdd_Successfully() { var user = new OurTraceUser() { UserName = "******", BirthDate = DateTime.Now, Country = "Bulgaria", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", NormalizedUserName = "******", PasswordHash = "00000" }; await this.dbContext.Users.AddAsync(user); await this.dbContext.SaveChangesAsync(); var notificationModel = new NotificationServiceModel() { Content = "NotificContent", Controller = "Group", Action = "Open", Username = user.UserName, ElementId = "1" }; await this.notificationService.AddNotificationToUserAsync(notificationModel); var expectedNotification = new Notification() { Content = "NotificContent", Controller = "Group", Action = "Open", User = user, ElementId = "1", Seen = false }; var actualNotificatiionsCount = await this.dbContext.Notifications.CountAsync(); var actualNotificatiion = await this.dbContext.Notifications.FirstOrDefaultAsync(); Assert.Equal(1, actualNotificatiionsCount); AssertExpectedAndActualNotificationProperties(expectedNotification, actualNotificatiion); }
public async Task AddNotificationToUserAsync(NotificationServiceModel notificationModel) { if (AllStringPropertiesAreFilled(notificationModel)) { var user = await this.identityService .GetUserByName(notificationModel.Username) .SingleOrDefaultAsync(); if (user != null) { var notification = automapper.Map <Notification>(notificationModel); notification.User = user; await this.dbContext.Notifications.AddAsync(notification); await this.dbContext.SaveChangesAsync(); } } }
public Result <NotificationServiceModel> Update(NotificationServiceModel notificationModel, int userId, int groupId) { var result = new Result <NotificationServiceModel>(); try { var notificationEntity = _unitOfWork.NotificationRepository.GetSingle(x => x.Id == notificationModel.Id && x.IsActive && x.UserId == userId && x.GroupId == groupId); if (notificationEntity != null) { notificationEntity.Description = notificationModel.Description; notificationEntity.Type = (int)notificationModel.Type; notificationEntity.UpdatedOn = Application.CurrentDate; notificationEntity.UpdatedBy = userId; _unitOfWork.Save(); var notificationServiceModel = _mapper.Map <NotificationServiceModel>(notificationEntity); result.IsSuccess = true; result.Data = notificationServiceModel; return(result); } else { result.IsSuccess = false; result.ErrorMessage = ErrorMessage.GetUnAuth; return(result); } } catch (Exception ex) { _logger.LogCritical(ex.InnerException.Message); result.IsSuccess = false; result.ErrorMessage = ErrorMessage.Update; return(result); } }