Exemplo n.º 1
0
        public IActionResult CreateDevice([FromBody] Device device)
        {
            if (device == null)
            {
                return(BadRequest(ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (deviceRepository.DeviceExists(device.Id) == true)
            {
                ModelState.AddModelError("", $"Device Type Id {device.Id} already exists");
                return(StatusCode(422, ModelState));
            }
            deviceRepository.CreateDevice(device);
            string         topic         = clientUserRepository.CreateTopic(device.Id, device.Topic);
            ClaimsIdentity claimIdentity = this.User.Identity as ClaimsIdentity;
            string         userId        = claimIdentity.FindFirst(ClaimTypes.Name).Value;
            ClientUser     client        = clientUserRepository.GetClientUser(userId);

            clientUserRepository.SubcribeTopic(client.Id, topic);
            clientService.SubscribeTopic(topic);
            return(Ok("Create Success"));
        }
Exemplo n.º 2
0
        public IActionResult Authenticate([FromBody] UserLoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            UserModel userModel = userService.Authenticate(model);
            string    userId    = userService.GetUserId(model);

            if (userModel == null)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                ClientUser    client = clientUserRepository.GetClientUser(userId);
                List <string> topics = client.Topics;
                if (topics != null)
                {
                    for (int i = 0; i < topics.Count; i++)
                    {
                        clientService.SubscribeTopic(topics[i]);
                    }
                }
                return(Ok(userModel));
            }
        }