public HttpResponseMessage NewAnnouncement([FromBody] AnnouncementCreateDTO announcement)
        {
            if (ModelState.IsValid)
            {
                Announcement entity = new Announcement
                {
                    CategoryId     = announcement.CategoryId,
                    Description    = announcement.Description,
                    Email          = announcement.Email,
                    Phonenumber    = announcement.Phonenumber,
                    Title          = announcement.Title,
                    PostDate       = DateTime.Now,
                    ExpirationDate = DateTime.Now.AddMonths(1),
                    Confirmed      = false
                };

                using (SummerCampDbContext ctx = new SummerCampDbContext())
                {
                    ctx.Announcements.Add(entity);
                    ctx.SaveChanges();
                }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, entity);
                //string uri = Url.Link("DefaultApi", new { Id = entity.AnnouncementId });
                //response.Headers.Location = new Uri(uri);
                return(response);
            }

            return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
        }
Beispiel #2
0
        public HttpResponseMessage NewReview([FromBody] ReviewCreateDTO review)
        {
            if (string.IsNullOrEmpty(review.Comment) && !review.Rating.HasValue)
            {
                ModelState.AddModelError("", "Rating or comment required");
            }

            if (ModelState.IsValid)
            {
                Review entity = new Review
                {
                    AnnouncementId = review.AnnouncementId,
                    Comment        = review.Comment,
                    Username       = review.Username,
                    Rating         = review.Rating,
                    Postdate       = DateTime.Now
                };

                using (SummerCampDbContext ctx = new SummerCampDbContext())
                {
                    ctx.Reviews.Add(entity);
                    ctx.SaveChanges();
                }

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, review);
                return(response);
            }

            HttpError error = new HttpError(ModelState, false);

            return(Request.CreateResponse(HttpStatusCode.BadRequest, error));
        }
        public HttpResponseMessage CloseAnnouncement([FromUri] int id, [FromBody] AnnouncementAuthDTO authInfo)
        {
            using (SummerCampDbContext ctx = new SummerCampDbContext())
            {
                Announcement announcement = ctx.Announcements.FirstOrDefault(a => a.AnnouncementId == id);

                if (string.Equals(announcement.Email, authInfo.Email, StringComparison.OrdinalIgnoreCase) && announcement.Confirmed)
                {
                    announcement.Closed = true;
                    ctx.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }

                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }
        }
        public HttpResponseMessage ExtendAnnouncement([FromUri] int id, [FromBody] AnnouncementAuthDTO authInfo)
        {
            using (SummerCampDbContext ctx = new SummerCampDbContext())
            {
                Announcement announcement = ctx.Announcements.FirstOrDefault(a => a.AnnouncementId == id);

                if (string.Equals(announcement.Email, authInfo.Email, StringComparison.OrdinalIgnoreCase))
                {
                    if (announcement.Closed && announcement.Confirmed)
                    {
                        return(Request.CreateResponse(HttpStatusCode.MethodNotAllowed));
                    }

                    announcement.ExpirationDate = announcement.ExpirationDate.AddMonths(1);
                    ctx.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }

                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }
        }