public async Task <IHttpActionResult> PutStatus(int id, UIStatusPost uiStatusPost) { string userId = User.Identity.GetUserId(); Status status = await db.Status.FindAsync(id); if (status == null) { return(BadRequest(string.Format("Status {0} doesn't exist.", id))); } if (!status.Creator.Id.Equals(userId)) { return(BadRequest("You don't own this status.")); } ApplicationUser applicationUser = db.Users.Find(userId); DateTime utc = DateTime.UtcNow; DateTimeOffset dateUtcOffset = new DateTimeOffset(utc, TimeSpan.Zero); TimeSpan offset = new TimeSpan( hours: 0, minutes: -uiStatusPost.TimezoneOffsetMinutes, // Negative sign is important. seconds: 0); DateTimeOffset userLocalDateOffset = dateUtcOffset.ToOffset(offset); DateTime localTime = userLocalDateOffset.DateTime; if (status.Creator.Id != userId) { return(Unauthorized()); } status.Text = uiStatusPost.Text; status.DateModifiedUtc = utc; status.DateModifiedLocal = localTime; db.Status.Attach(status); var entry = db.Entry(status); entry.Property(e => e.Text).IsModified = true; entry.Property(e => e.DateModifiedUtc).IsModified = true; entry.Property(e => e.DateModifiedLocal).IsModified = true; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StatusExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }
public async Task <IHttpActionResult> PostStatus(int groupId, UIStatusPost uiStatusPost) { string userId = User.Identity.GetUserId(); Group group = await db.Groups.FindAsync(groupId); if (group == null) { return(BadRequest(string.Format("Group {0} doesn't exit.", groupId))); } if (!group.Members.Any(x => x.Id.Equals(userId))) { return(BadRequest("You're not in this group.")); } ApplicationUser applicationUser = db.Users.Find(userId); DateTime utc = DateTime.UtcNow; DateTimeOffset dateUtcOffset = new DateTimeOffset(utc, TimeSpan.Zero); TimeSpan offset = new TimeSpan( hours: 0, minutes: uiStatusPost.TimezoneOffsetMinutes, seconds: 0); DateTimeOffset userLocalDateOffset = dateUtcOffset.ToOffset(offset); DateTime localTime = userLocalDateOffset.DateTime; // Update user's location. LocationService.UpdateUserLocation( applicationUser, uiStatusPost.Position, uiStatusPost.TimezoneOffsetMinutes); Status status = new Status() { CreatorId = User.Identity.GetUserId(), GroupId = group.GroupId, Text = uiStatusPost.Text, DateCreatedUtc = utc, DateCreatedLocal = localTime, Creator = applicationUser, City = applicationUser.City, Country = applicationUser.Country, Latitude = applicationUser.Latitude, Longitude = applicationUser.Longitude, Group = group, }; status.StatusViews = ControllerUtils.MakeStatusViews(userId, group, status, utc); db.Status.Add(status); db.Users.Attach(applicationUser); var entry = db.Entry(applicationUser); entry.Property(e => e.Latitude).IsModified = true; entry.Property(e => e.Longitude).IsModified = true; entry.Property(e => e.TimezoneOffsetMinutes).IsModified = true; entry.Property(e => e.City).IsModified = true; entry.Property(e => e.Country).IsModified = true; await db.SaveChangesAsync(); UIStatus uiStatus = new UIStatus(); uiStatus.Id = status.StatusId; uiStatus.DateCreatedUtc = status.DateCreatedUtc; uiStatus.DateCreatedLocal = status.DateCreatedLocal; uiStatus.CreatorName = status.Creator.Nickname; uiStatus.CreatorId = status.Creator.Id; uiStatus.IsCreatedByUser = true; uiStatus.IsRtl = ControllerUtils.IsRtl(uiStatusPost.Text); uiStatus.Views = ControllerUtils.GetUIStatusViews(status.StatusViews); uiStatus.Text = status.Text; uiStatus.City = status.City; uiStatus.Country = status.Country; return(Ok(uiStatus)); }