Beispiel #1
0
        public async Task <IHttpActionResult> PutComment(int id, Comment comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != comment.Id)
            {
                return(BadRequest());
            }

            db.Entry(comment).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public async Task <IHttpActionResult> PostJoin(Join join)
        {
            join.At = DateTime.Now;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Joins.Add(join);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (JoinExists(join.MeetupId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = join.MeetupId }, join));
        }
Beispiel #3
0
        public async Task <IHttpActionResult> PostFollow(Follow follow)
        {
            if (follow.FollowingUserId != User.Identity.GetUserId())
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            follow.At = DateTime.Now;
            if (!ModelState.IsValid || follow.FollowingUserId == follow.FollowedUserId)
            {
                return(BadRequest(ModelState));
            }

            db.Follows.Add(follow);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (FollowExists(follow.FollowedUserId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = follow.FollowedUserId }, follow));
        }
Beispiel #4
0
        public async Task <IHttpActionResult> PutMeetup(Meetup updated)
        {
            if (updated.Title == null ||
                updated.When == null ||
                updated.Where == null ||
                updated.Description == null)
            {
                return(BadRequest());
            }

            if (updated.Title != null && updated.Title.Length > 200)
            {
                return(BadRequest(ModelState));
            }
            var meetup = await db
                         .Meetups
                         .Where(m => m.IsCancelled == false)
                         .FirstOrDefaultAsync(m => m.Id == updated.Id);

            if (meetup == null)
            {
                return(NotFound());
            }
            if (meetup.Sponsor != User.Identity.GetUserId())
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }

            meetup.Description = updated.Description ?? meetup.Description;
            meetup.Title       = updated.Title ?? meetup.Title;
            meetup.Where       = updated.Where ?? meetup.Where;
            if (updated.When != default(DateTime))
            {
                meetup.When = updated.When;
            }
            meetup.UpdatedAt = DateTime.Now;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(meetup));
        }
Beispiel #5
0
        public async Task <IHttpActionResult> Put(PrivateUserViewModel user)
        {
            if (user.UserId != LoggedInUserId)
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            var profile = await db.Profiles.FirstOrDefaultAsync(p => p.UserId == user.UserId);

            if (profile == null)
            {
                return(NotFound());
            }

            profile.Gender     = user.Gender;
            profile.FamilyName = user.FamilyName;
            profile.GivenName  = user.GivenName;
            profile.NickName   = user.NickName;
            profile.Picture    = user.Picture;
            profile.Brief      = user.Brief;
            profile.UpdatedAt  = DateTime.Now;

            db.Entry(profile).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #6
0
        public async Task <IHttpActionResult> PutMeetup(Meetup meetup)
        {
            // Ensure it's post by sponser
            if (meetup.Sponsor != User.Identity.GetUserId())
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }
            // Ensure valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Ensure existing
            var old = await db.Meetups.FirstOrDefaultAsync(m => m.Id == meetup.Id);

            if (old == null)
            {
                return(NotFound());
            }

            // Update and save
            old.Description     = meetup.Description;
            old.Title           = meetup.Title;
            old.When            = meetup.When;
            old.Where           = meetup.Where;
            meetup.UpdatedAt    = DateTime.Now;
            db.Entry(old).State = EntityState.Modified;
            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #7
0
        public async Task <ActionResult> Create(Meetup meetup)
        {
            meetup.Sponsor   = User.Identity.GetUserId();
            meetup.CreatedAt = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Meetups.Add(meetup);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(meetup));
        }
Beispiel #8
0
 async Task UpdateProfileTableAsync(string userId, string pictureUrl)
 {
     using (var db = new MuDbContext())
     {
         db.Profiles.Find(userId).Picture = pictureUrl;
         try
         {
             await db.SaveChangesAsync();
         }
         catch (Exception e)
         {
             throw e;
         }
     }
 }