示例#1
0
        public async Task <IActionResult> PostTask([FromBody] TaskDto taskDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Plug plug = await _context.Plugs.FindAsync(taskDto.DeviceMac);

            if (plug == null)
            {
                return(NotFound(Error.PlugDoesNotExist));
            }

            if (UserOwnershipValidator.IsNotValidated(_currentUsername, plug, _context))
            {
                return(Unauthorized(Error.UnauthorizedOwner));
            }

            Models.Task task = _mapper.Map <Models.Task>(taskDto);
            plug.AddTask(task, _context);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetTasks", new { mac = task.DeviceMac }, task));
        }