Exemplo n.º 1
0
        // GET api/<controller>/5
        private IQueryable <BeaconResult> GetById(Guid id)
        {
            URL urlToAssign = UrlSelection.SelectUrl(id);

            var result = db.Beacons.Select(b => new BeaconResult
            {
                Id             = b.Id,
                Url            = urlToAssign.UrlValue,
                Notification   = b.Notification,
                BeaconID       = b.BeaconID,
                Major          = b.Major,
                Minor          = b.Minor,
                DisplayTimeout = _timeout
            }).Where(x => x.Id == id);

            Log logRecord = new Log
            {
                Beacon_Id   = id,
                RequestDate = DateTime.Now,
                Url_Id      = urlToAssign.Id
            };

            db.Log.Add(logRecord);
            db.SaveChanges();

            return(result);
        }
Exemplo n.º 2
0
        public IQueryable <BeaconResult> Get([FromUri] Guid id, [FromUri] string username = "", [FromUri] string password = "", [FromUri] string deviceId = "")
        {
            // Beacon validation
            if (!db.Beacons.Where(x => x.Id == id).Any())
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent("No Beacon found")
                });
            }

            // If personal parameters empty, call GetById method
            if ((string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) && string.IsNullOrEmpty(deviceId))
            {
                return(GetById(id));
            }

            User user = db.User.Where(x => x.UserName == username && x.Password == password || x.DeviceId == deviceId)
                        .Include(y => y.Group).FirstOrDefault();

            // User validation
            if (user == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent("No User found")
                });
            }

            // Url assignment
            URL urlToAssign = UrlSelection.SelectUrl(user, id);

            var result = db.Beacons.Select(b => new BeaconResult
            {
                Id             = b.Id,
                Url            = urlToAssign.UrlValue,
                Notification   = b.Notification,
                Group          = user.Group.Name,
                BeaconID       = b.BeaconID,
                Major          = b.Major,
                Minor          = b.Minor,
                DisplayTimeout = _timeout,
                DeviceId       = user.DeviceId
            }).Where(x => x.Id == id);

            Log logRecord = new Log
            {
                Beacon_Id   = id,
                User_Id     = user.Id,
                Group_Id    = user.Group.ID,
                RequestDate = DateTime.Now,
                Url_Id      = urlToAssign.Id
            };

            db.Log.Add(logRecord);
            db.SaveChanges();

            return(result);
        }