Esempio n. 1
0
        public async Task <IActionResult> Index(CancellationToken ct)
        {
            var id = Request.Headers["ghosts-id"];

            log.Trace($"Request by {id}");

            var m = new Machine();

            if (!string.IsNullOrEmpty(id))
            {
                m = await _service.GetByIdAsync(new Guid(id), ct);
            }

            if (m == null || !m.IsValid())
            {
                m = await _service.FindByValue(WebRequestReader.GetMachine(HttpContext), ct);
            }

            if (m == null || !m.IsValid())
            {
                m = WebRequestReader.GetMachine(HttpContext);

                m.History.Add(new Machine.MachineHistoryItem {
                    Type = Machine.MachineHistoryItem.HistoryType.Created
                });
                await _service.CreateAsync(m, ct);
            }

            if (!m.IsValid())
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, "Invalid machine request"));
            }

            m.History.Add(new Machine.MachineHistoryItem {
                Type = Machine.MachineHistoryItem.HistoryType.RequestedId
            });
            await _service.UpdateAsync(m, ct);

            //client saves this for future calls
            return(Json(m.Id));
        }