public NotificationModel AddNotification(NotificationModel notificationModel) { //todo : add a better way of converting the dtos -> entities (autommaper) if (notificationModel.Type == Common.Models.Enums.EventType.AppointmentCancelled) { var canceledNotificationdata = (notificationModel.Data as JObject).ToObject <CanceledNotificationData>(); var entity = new NotificationEntity() { EventType = notificationModel.Type, FirstName = canceledNotificationdata.FirstName, AppointmentDateTime = canceledNotificationdata.AppointmentDateTime, OrganisationName = canceledNotificationdata.OrganisationName, Reason = canceledNotificationdata.Reason, CreationTime = DateTime.Now }; _dbContext.Notifications.Add(entity); _dbContext.SaveChanges(); notificationModel.Id = entity.Id; return(notificationModel); } // we failed to find all suitable data inside the model , then throw an error // todo : add a better error handling with base class and domain errors ... throw new ArgumentException($"Invalid input provided", nameof(NotificationModel)); }
public UserNotification AddNotification(NotificationModel notificationModel) { // todo add coherent error handling var notificationType = notificationModel.Type; var template = _dbContext.Set <TemplateEntity>().FirstOrDefault(x => x.EventType == notificationType); if (template != null) { var entity = new UserNotificationEntity() { Body = FillTemplate(notificationModel.Data as JObject, template.Body), Title = FillTemplate(notificationModel.Data as JObject, template.Title), EventType = notificationModel.Type, UserId = notificationModel.UserId }; _dbContext.Set <UserNotificationEntity>().Add(entity); _dbContext.SaveChanges(); return(Map <UserNotificationEntity, UserNotification>(entity)); } //something is wrong ?!? throw new Exception("Unhandled case ..."); }
public static void PopulateTestData(NotificationsDbContext dbContext) { var userGuid = Guid.Parse("b8412641-6436-49cc-816b-49b4a4f4ecd3"); dbContext.Notifications.Add(new NotificationEntity(userGuid, "title1", "text1")); dbContext.Notifications.Add(new NotificationEntity(userGuid, "title2", "text3")); dbContext.Templates.Add(new TemplateEntity { Body = "Hi {Firstname}, your appointment with {OrganisationName} at {AppointmentDateTime} has been - cancelled for the following reason: {Reason}.", Title = "Appointment Cancelled" }); dbContext.SaveChanges(); }
public NotificationControllerTest() { _dbOptions = new DbContextOptionsBuilder <NotificationsDbContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; using (var dbContext = new NotificationsDbContext(_dbOptions)) { dbContext.AddRange(GetNoticationSeed()); dbContext.Add(GetTemplateData()); dbContext.SaveChanges(); } }
private static void AddNotifications(NotificationsDbContext db, out Notification notification1, out Notification notification2, out Notification notification3) { notification1 = new Notification { NotificationId = 1, UserId = 1, CreatedAt = DateTime.Today - TimeSpan.FromDays(1) }.SetStringProperties(); notification2 = new Notification { NotificationId = 2, UserId = 1, CreatedAt = DateTime.Today - TimeSpan.FromDays(2) }.SetStringProperties(); notification3 = new Notification { NotificationId = 3, UserId = 1, CreatedAt = DateTime.Today - TimeSpan.FromDays(3), Subject = "Test" }; db.AddRange(notification1, notification2, notification3); db.SaveChanges(); }
public async Task Post_CreatesAndPersistsANewNotification() { // Arrange dbContext.NotificationTemplates.Add(new NotificationTemplateEntity { Id = Guid.NewGuid(), EventType = NotificationEventType.AppointmentCancelled, Body = "Hi {Firstname}, your appointment with {OrganisationName} at {AppointmentDateTime} has been - cancelled for the following reason: {Reason}.", Title = "Appointment Cancelled" }); dbContext.SaveChanges(); var notificationsBeforePost = dbContext.Notifications.Count(); var eventBody = new EventModel { EventType = NotificationEventType.AppointmentCancelled, UserId = Guid.NewGuid(), Data = new EventDataModel { AppointmentDateTime = DateTime.UtcNow, Firstname = "Mr T", OrganisationName = "The A Team", Reason = "Too many fools pitied" } }; var requestContent = new StringContent(JsonConvert.SerializeObject(eventBody, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }), Encoding.UTF8, "application/json"); // Act var response = await client.PostAsync(notificationsUri, requestContent); // Assert response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var responseModel = JsonConvert.DeserializeObject <NotificationModel>(content); Assert.NotEqual(Guid.Empty, responseModel.Id); Assert.Contains(eventBody.Data.Reason, responseModel.Body); Assert.Equal(notificationsBeforePost + 1, dbContext.Notifications.Count()); }
public void AddNotification(NotificationModel notification) { dbContext.Notifications .Add(new NotificationEntity(notification.UserId, notification.Title, notification.Text)); dbContext.SaveChanges(); }