예제 #1
0
        public async Task <IActionResult> Index([FromBody] string timeline, CancellationToken ct)
        {
            var id = Request.Headers["ghosts-id"];

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

            var m = WebRequestReader.GetMachine(HttpContext);

            if (!string.IsNullOrEmpty(id))
            {
                m.Id = new Guid(id);
                await _machineService.CreateAsync(m, ct);
            }
            else
            {
                if (!m.IsValid())
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, "Invalid machine request"));
                }
            }

            Timeline tl;

            try
            {
                tl = JsonConvert.DeserializeObject <Timeline>(timeline);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(StatusCodes.Status400BadRequest, "Invalid timeline file"));
            }

            return(Ok(await _service.CreateAsync(m, tl, ct)));
        }
예제 #2
0
        public async Task <IActionResult> PostMachine([FromBody] Machine machine, CancellationToken ct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var id = await _service.CreateAsync(machine, ct);

            return(CreatedAtAction("GetMachine", new { id }, machine));
        }
예제 #3
0
        public async Task <IActionResult> Create([FromForm] MachineEditViewModel machine)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _machineService.CreateAsync(_mapper.Map <Machine>(machine));

            return(RedirectToAction(nameof(Index)));
        }
예제 #4
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));
        }