public IActionResult Put([FromBody] ControllerViewModel model)
        {
            //Перевіряємо модель
            if (model == null || !ModelState.IsValid)
            {
                return(StatusCode(500, new InternalServerError("Data is incorrect!")));
            }
            var controller = _context.Controllers.Where(p => p.MAC == model.MAC).FirstOrDefault();

            if (controller != null)
            {
                return(StatusCode(500, new InternalServerError("Controller is already exists")));
            }
            if (model.MAC.Length != 12)
            {
                return(StatusCode(500, new InternalServerError("MAC address must has 12 symbols!")));
            }
            //Беремо айді користувача з токену
            var user = User.GetUser(_context);

            if (user == null)
            {
                return(Unauthorized(new UnauthorizedError()));
            }
            //Створюємо контролер
            controller = model.Adapt <Data.Models.Controller>();
            controller.InstalledDate = DateTime.Now;
            controller.PublicKey     = "jak to bedzie pracowac, pliz ktos wie?";
            _context.Controllers.Add(controller);
            //Створюємо UserHasController
            var userHasController = new UserHasController();

            userHasController.ControllerId = controller.Id;
            userHasController.UserId       = user.Id;
            userHasController.IsAdmin      = true;
            _context.UserHasControllers.Add(userHasController);
            //Зберігаємо зміни
            _context.SaveChanges();
            //Створюємо віртуальний пристрій
            var device = new Device
            {
                Name         = "Notification",
                Pin          = 0,
                MAC          = null,
                Status       = true,
                ControllerId = controller.Id,
                DeviceTypeId = _context.DeviceTypes.Where(p => p.TypeName == "Virtual").FirstOrDefault().Id,
            };

            _context.Devices.Add(device);
            _context.SaveChanges();
            //Ствоюємо UserasDevice
            var userHasDevice = new UserHasDevice
            {
                DeviceId = device.Id,
                UsersHaveControllerId = userHasController.Id
            };

            _context.UserHasDevices.Add(userHasDevice);
            _context.SaveChanges();
            //Повертаємо створений контролер як резльтат
            return(new JsonResult(controller.Adapt <ControllerViewModel>()));
        }
        public IActionResult Put([FromBody] DeviceViewModel model)
        {
            if (model == null)
            {
                return(StatusCode(500, new InternalServerError()));
            }

            if (model.MAC != null && model.MAC.Length != 12)
            {
                return(StatusCode(500, new InternalServerError("MAС address must be 12 characters long!")));
            }

            var user = User.GetUser(_context);

            var userHasController = _context
                                    .UserHasControllers
                                    .Where(p => p.ControllerId == model.ControllerId)
                                    .Where(p => p.UserId == user.Id)
                                    .FirstOrDefault();

            if (userHasController == null || !userHasController.IsAdmin)
            {
                return(Unauthorized(new UnauthorizedError()));
            }

            var devicesWithCommonMacAddress = _context
                                              .Devices
                                              .Where(p => p.ControllerId == userHasController.ControllerId)
                                              .Where(p => p.MAC != null && p.MAC == model.MAC);

            if (devicesWithCommonMacAddress.Any())
            {
                return(StatusCode(500, new InternalServerError("Mac Address is already exists!")));
            }

            var devicesWithCommonPin = _context
                                       .Devices
                                       .Where(p => p.ControllerId == userHasController.ControllerId)
                                       .Where(p => p.Pin != 0 && p.Pin == model.Pin);

            var sensorsWithCommonPin = _context
                                       .Sensors
                                       .Where(p => p.ControllerId == userHasController.ControllerId)
                                       .Where(p => p.Pin != 0 && p.Pin == model.Pin);

            if (devicesWithCommonPin.Any() || sensorsWithCommonPin.Any())
            {
                return(StatusCode(500, new InternalServerError("Pin is already taken!")));
            }

            var device = new Device()
            {
                Name         = model.Name,
                DeviceTypeId = model.DeviceTypeId,
                Pin          = model.Pin,
                MAC          = model.MAC,
                ControllerId = model.ControllerId,
                Status       = model.Status
            };

            _context.Devices.Add(device);

            var userHasDevice = new UserHasDevice
            {
                UsersHaveControllerId = userHasController.Id,
                DeviceId = device.Id
            };

            _context.UserHasDevices.Add(userHasDevice);

            _context.SaveChanges();

            device.DeviceType = _context.DeviceTypes.Find(device.DeviceTypeId);

            return(Json(device.Adapt <DeviceViewModel>()));
        }
Example #3
0
        public IActionResult AccessDevice([FromBody] DeviceAccessViewModel model)
        {
            var user = User.GetUser(_context);

            var device = _context.Devices.Find(model.DeviceId);

            if (device == null)
            {
                return(NotFound(new NotFoundError("Device not found!")));
            }

            var accessUser = _context
                             .Users
                             .Where(p => p.Email == model.UserName || p.UserName == model.UserName)
                             .FirstOrDefault();

            if (accessUser == null)
            {
                return(NotFound(new NotFoundError("A user with such login or email does not exist.")));
            }

            var userHasController = _context.UserHasControllers
                                    .Where(p => p.ControllerId == device.ControllerId)
                                    .Where(p => p.UserId == user.Id)
                                    .Where(p => p.IsAdmin)
                                    .FirstOrDefault();

            if (userHasController == null)
            {
                return(Unauthorized(new UnauthorizedError()));
            }

            var accessUserHasController = _context.UserHasControllers
                                          .Where(p => p.ControllerId == device.ControllerId)
                                          .Where(p => p.UserId == accessUser.Id)
                                          .FirstOrDefault();

            if (accessUserHasController == null)
            {
                return(Unauthorized(new UnauthorizedError("User hasn`t access to your controller!")));
            }

            var userHasDevice = _context
                                .UserHasDevices
                                .Where(p => p.DeviceId == device.Id)
                                .Where(p => p.UsersHaveControllerId == accessUserHasController.Id)
                                .FirstOrDefault();

            if (userHasDevice != null)
            {
                return(StatusCode(500, new InternalServerError("User have been alerady added")));
            }

            var accessUserHasDevice = new UserHasDevice
            {
                DeviceId = device.Id,
                UsersHaveControllerId = accessUserHasController.Id
            };

            _context.UserHasDevices.Add(accessUserHasDevice);
            _context.SaveChanges();
            accessUserHasDevice.UserHasController      = _context.UserHasControllers.Find(accessUserHasDevice.UsersHaveControllerId);
            accessUserHasDevice.UserHasController.User = _context.Users.Find(accessUserHasDevice.UserHasController.UserId);

            return(Json(accessUserHasDevice.Adapt <UserHasDeviceViewModel>()));
        }