Exemple #1
0
        public async Task <IActionResult> Cancel([FromRoute] string deviceId)
        {
            if (!await _deviceService.ExistsAsync(deviceId))
            {
                return(BadRequest("Device does not exist"));
            }

            var lastAlert = _alertService.GetAll()
                            .Where(a => a.DeviceId == deviceId)
                            .OrderByDescending(a => a.Timestamp)
                            .FirstOrDefault();

            //var lastAlert = (await _deviceService.GetByIdAsync(deviceId))
            //    .Alerts
            //    .Where(a => a.Cancelled == false)
            //    .OrderByDescending(a => a.Timestamp)
            //    .FirstOrDefault();

            if (lastAlert == null || lastAlert.Cancelled == true)
            {
                return(BadRequest("There is no previous non-cancelled alert"));
            }

            lastAlert.Cancelled = true;
            await _alertService.UpdateAsync(lastAlert);

            return(Ok());
        }