コード例 #1
0
        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));
        }
コード例 #2
0
        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 ...");
        }
コード例 #3
0
ファイル: SeedData.cs プロジェクト: josep-almolda/assesment
        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();
        }
コード例 #4
0
        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();
            }
        }
コード例 #5
0
 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();
 }
コード例 #6
0
        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());
        }
コード例 #7
0
 public void AddNotification(NotificationModel notification)
 {
     dbContext.Notifications
     .Add(new NotificationEntity(notification.UserId, notification.Title, notification.Text));
     dbContext.SaveChanges();
 }