Ejemplo n.º 1
0
        public async Task <IHttpActionResult> GetStatuses(int groupId, DateTime sinceWhen)
        {
            string          userId = User.Identity.GetUserId();
            ApplicationUser me     = db.Users.Find(userId);

            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."));
            }

            string myName = User.Identity.GetUserName();

            var statuses = await db.Status
                           .Where(s => s.GroupId == group.GroupId && s.DateCreatedUtc > sinceWhen)
                           .Select(s => s)
                           .OrderByDescending(s => s.DateCreatedUtc)
                           .Take(QueryPageSize)
                           .ToListAsync();

            UIStatus[] uiStatuses = statuses.Select(
                s => new UIStatus()
            {
                Id                = s.StatusId,
                CreatorName       = s.Creator.Nickname,
                CreatorId         = s.Creator.Id,
                DateCreatedUtc    = s.DateCreatedUtc,
                DateCreatedLocal  = s.DateCreatedLocal,
                DateModifiedUtc   = s.DateModifiedUtc,
                DateModifiedLocal = s.DateModifiedLocal,
                Text              = s.Text,
                IsRtl             = ControllerUtils.IsRtl(s.Text),
                City              = s.City,
                IsCreatedByUser   = s.Creator.UserName.Equals(myName),
                Views             = ControllerUtils.GetUIStatusViews(s.StatusViews)
            }).ToArray();

            // Record group visit.
            LastGroupVisit lastVisit = me.LastGroupVisits.Where(x => x.GroupId == groupId).FirstOrDefault();

            if (lastVisit == null)
            {
                db.LastGroupVisits.Add(new LastGroupVisit()
                {
                    GroupId      = groupId,
                    Group        = group,
                    Visitor      = me,
                    VisitorId    = me.Id,
                    VisitTimeUtc = DateTime.UtcNow
                });
                await db.SaveChangesAsync();
            }
            else
            {
                lastVisit.VisitTimeUtc = DateTime.UtcNow;
                db.LastGroupVisits.Attach(lastVisit);
                db.Entry(lastVisit).Property(x => x.VisitTimeUtc).IsModified = true;
                await db.SaveChangesAsync();
            }

            return(Ok(uiStatuses));
        }
Ejemplo n.º 2
0
        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));
        }