コード例 #1
0
        public IHttpActionResult GetOldest()
        {
            // TODO: refactor extract method
            var currentUserId = this.User.Identity.GetUserId();
            var currentUser = this.data
              .Users
              .All()
              .Where(x => x.Id == currentUserId)
              .FirstOrDefault();

            if (currentUser == null)
            {
                return BadRequest("Invalid user authentication");
            }

            var notification = currentUser.Notifications
                .OrderByDescending(x => x.DateCreated)
                .FirstOrDefault();

            if (notification == null)
            {
                return Ok(new NotificationModel[0]);
            }

            notification.IsRead = true;
            this.data.SaveChanges();

            var modelled = new NotificationModel()
            {
                DateCreated = notification.DateCreated,
                GameId = notification.GameId,
                Id = notification.Id,
                Message = notification.Message,
                State = (notification.IsRead == true ? "Read" : "Unread"),
                Type = notification.Type,
            };


            return Ok(modelled);

        }