public void UpdateAnnouncement(int id, AnnouncementFormData data)
        {
            var announcement = context.Announcements.Find(id);

            announcement.Title       = data.Title;
            announcement.Description = data.Description;
            context.SaveChanges();
        }
        public Announcement createAnnouncement(AnnouncementFormData data)
        {
            Announcement announcement = new Announcement()
            {
                Title       = data.Title,
                Description = data.Description,
                CreateDate  = DateTime.Now,
                Creator     = context.Users.Find(user_service.GetUserId())
            };

            context.Announcements.Add(announcement);
            context.SaveChanges();

            return(announcement);
        }
Example #3
0
        public void AnnounementFormDataValidation_IsInvalid_ForInvalidData()
        {
            var data = new AnnouncementFormData()
            {
                Title       = "",
                Description = null
            };

            FormDataValidationResult result = data.Validate();

            // data should be considered invalid
            Assert.False(result.IsValid);

            // two error messages should be set
            Assert.Equal(2, result.ErrorMessages.Count);
        }
Example #4
0
        public void AnnounementFormDataValidation_IsValid_ForValidData()
        {
            var data = new AnnouncementFormData()
            {
                Title       = "Hello there",
                Description = "Testing 1 2 3"
            };


            FormDataValidationResult result = data.Validate();

            // data should be considered valid
            Assert.True(result.IsValid);

            // no error message should be set
            Assert.Empty(result.ErrorMessages);
        }
        public IHttpActionResult EditAnnouncement(int id, [FromBody] AnnouncementFormData data)
        {
            if (!user_service.IsAuthorizedToEditAnnouncements(id))
            {
                return(Unauthorized());
            }

            // for preflight requests
            if (RequestUtility.IsPreFlightRequest(Request))
            {
                return(Ok());
            }

            var result = data.Validate();

            if (!result.IsValid)
            {
                return(new BadHttpRequest(result.ErrorMessages));
            }

            announcement_repository.UpdateAnnouncement(id, data);
            return(Ok());
        }
        public IHttpActionResult Create([FromBody] AnnouncementFormData data)
        {
            if (!user_service.IsAuthorizedToCreateAnnouncements())
            {
                return(Unauthorized());
            }

            // for preflight requests
            if (RequestUtility.IsPreFlightRequest(Request))
            {
                return(Ok());
            }

            var result = data.Validate();

            if (!result.IsValid)
            {
                return(new BadHttpRequest(result.ErrorMessages));
            }

            var announcement = announcement_repository.createAnnouncement(data);

            return(Ok(new AnnouncementsResponseData(announcement)));
        }