public bool ChangeStatusBySerialNo(string serialNo, DeviceStatus newStatus, int userId)
        {
            var ctx    = new PcPoolEntities();
            var device = ctx.DeviceInstances.FirstOrDefault(di => di.SeriaNo == serialNo);

            return(device != null && UpdateDeviceStatusAndHistory(newStatus, userId, device, ctx));
        }
예제 #2
0
        public Model.User AddNewUser(Model.User user)
        {
            var ctx = new PcPoolEntities();

            try
            {
                var newUser = ctx.Users.Add(new PcPoolModels.User()
                {
                    UserTypeID  = (int)UserType.User,
                    LastName    = user.LastName,
                    FirstName   = user.FirstName,
                    UserName    = user.UserName,
                    Title       = user.Title,
                    Address     = user.Address,
                    Designation = user.Designation,
                    Email       = user.Email,
                    //Password = user.Password
                });
                ctx.SaveChanges();
                return(ConvertToModel(newUser));
            }
            catch (Exception ex)
            {
                return(null);
            }
            //return true;
        }
        public bool ChangeStatusByRfid(string rfid, DeviceStatus newStatus, int userId)
        {
            var ctx    = new PcPoolEntities();
            var device = ctx.DeviceInstances.FirstOrDefault(di => di.RFID == rfid);

            return(device != null && UpdateDeviceStatusAndHistory(newStatus, userId, device, ctx));
        }
예제 #4
0
        public List <Model.User> GetAllUsers()
        {
            var ctx   = new PcPoolEntities();
            var users = ctx.Users.Select(ConvertToModel).Where(u => u.UserType != UserType.SuperAdmin).ToList();

            return(users);
            //return null;
        }
        public List <DeviceType> GetDeviceTypes()
        {
            var ctx           = new PcPoolEntities();
            var dbDeviceTypes = ctx.DeviceTypes.Select(dt => new DeviceType()
            {
                DeviceName   = dt.DevicaeName,
                DeviceTypeId = dt.DeviceTypeID
            }).ToList();

            return(dbDeviceTypes);
        }
예제 #6
0
        static void Main(string[] args)
        {
            var pc   = new PcPoolEntities();
            var user = new PcPoolModels.User()
            {
                FirstName = "23"
            };

            pc.Users.Add(user);
            pc.SaveChanges();
        }
 private void AddDeviceStatusHistory(PcPoolEntities ctx, int deviceInstanceId, DeviceStatus newDeviceStatus, int userId)
 {
     //var ctx=new PcPoolEntities();
     ctx.DeviceStatusHistories.Add(new PcPoolModels.DeviceStatusHistory()
     {
         DeviceInstanceID = deviceInstanceId,
         ModificationDate = DateTime.Now,
         ModifiedByUserID = userId,
         NewStatusID      = (int)newDeviceStatus
     });
 }
        private static int GetNumberOfReservedDevices(int deviceTypeId, PcPoolEntities ctx, int userId = -1)
        {
            var reservationList =
                ctx.ReservationLists.Where(
                    s =>
                    s.DeviceTypeID == deviceTypeId && s.EndDate > DateTime.UtcNow &&
                    (userId == -1 || s.UserID != userId));

            var reserved = reservationList.Any() ? reservationList.Sum(rl => rl.Amount) : 0;

            return(reserved);
        }
예제 #9
0
        public Model.User VerifyUser(string userName, string password)
        {
            var ctx = new PcPoolEntities();

            try
            {
                var user = ctx.Users.FirstOrDefault(u => u.UserName == userName /*&& u.Password == password*/);
                return(ConvertToModel(user));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public bool AddnewItemType(DeviceType newType)
        {
            var ctx     = new PcPoolEntities();
            var dbModel = new PcPoolModels.DeviceType()
            {
                DevicaeName       = newType.DeviceName,
                DeviceDescription = newType.Description,
                DeviceModel       = newType.Model,
                DeviceVersion     = newType.Version
            };

            ctx.DeviceTypes.Add(dbModel);
            ctx.SaveChanges();
            return(true);
        }
예제 #11
0
        private User DbUserData(string userName)
        {
            var ctx    = new PcPoolEntities();
            var userDb = ctx.Users.FirstOrDefault(u => u.UserName == userName);
            var user   = userDb == null
                ? null
                : new User()
            {
                UserName = userDb.UserName,
                UserId   = userDb.UserID,
                UserType = (Model.UserType)userDb.UserTypeID
            };

            return(user);
        }
        public IList <DeviceInstance> GetAll(DeviceStatus deviceStatus = DeviceStatus.None, int deviceTypeId = 0)
        {
            var ctx = new PcPoolEntities();

            var devices = GetDeviceDetails(ctx);

            if (deviceStatus != DeviceStatus.None)
            {
                devices = devices.Where(d => d.DeviceStatus == deviceStatus);
            }
            if (deviceTypeId != 0)
            {
                devices = devices.Where(d => d.DeviceTypeId == deviceTypeId);
            }
            return(devices.ToList());
        }
        public IList <InventoryItemStat> GetInventoryStatus()
        {
            using (var ctx = new PcPoolEntities())
            {
                var devicesTypes = ctx.DeviceTypes;

                var result = new List <InventoryItemStat>();
                try
                {
                    foreach (var deviceType in devicesTypes)
                    {
                        //TODO: Check with the product owner if we should subtract reserved devices from instock count
                        var inStock = ctx.
                                      DeviceInstances.
                                      Count(di => di.DeviceTypeID == deviceType.DeviceTypeID &&
                                            di.DeviceStatusID.Value == (int)DeviceStatus.InStock);

                        var loaned = ctx.
                                     DeviceInstances.
                                     Count(di => di.DeviceTypeID == deviceType.DeviceTypeID &&
                                           di.DeviceStatusID.Value == (int)DeviceStatus.Loaned);

                        var reservedCount = GetNumberOfReservedDevices(deviceType.DeviceTypeID, ctx);
                        var total         = ctx.DeviceInstances.
                                            Count(di => di.DeviceTypeID == deviceType.DeviceTypeID);

                        result.Add(new InventoryItemStat()
                        {
                            Loaned        = loaned,
                            InStock       = inStock,
                            Total         = total,
                            ItemName      = deviceType.DevicaeName,
                            ReservedCount = reservedCount
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                return(result);
            }
        }
        private static IQueryable <DeviceInstance> GetDeviceDetails(PcPoolEntities ctx)
        {
            var devices = ctx.DeviceInstances.Join(ctx.DeviceTypes,
                                                   di => di.DeviceTypeID,
                                                   dt => dt.DeviceTypeID,
                                                   (di, dt) => new DeviceInstance()
            {
                DeviceName       = dt.DevicaeName,
                DeviceTypeId     = dt.DeviceTypeID,
                Description      = di.Description,
                DescriptionTitle = di.DescriptionTitle,
                Id = di.DeviceInstanceID,
                ManufacturingYear = di.ManufacturingYear.Value,
                Model             = dt.DeviceModel,
                DeviceStatus      = (DeviceStatus)di.DeviceStatusID.Value,
                SeriaNo           = di.SeriaNo,
                RFID = di.RFID
            });

            return(devices);
        }
        public bool AddnewItem(DeviceInstance deviceInstance)
        {
            var ctx = new PcPoolEntities();

            try
            {
                ctx.DeviceInstances.Add(new PcPoolModels.DeviceInstance()
                {
                    DeviceTypeID      = deviceInstance.DeviceTypeId,
                    Description       = deviceInstance.Description,
                    DescriptionTitle  = deviceInstance.DescriptionTitle,
                    SeriaNo           = deviceInstance.SeriaNo,
                    RFID              = deviceInstance.RFID,
                    DeviceStatusID    = (int)deviceInstance.DeviceStatus,
                    ManufacturingYear = deviceInstance.ManufacturingYear
                });
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(true);
        }
예제 #16
0
        public void ChangeUserType(int userId)
        {
            using (var ctx = new PcPoolEntities())
            {
                var user = ctx.Users.FirstOrDefault(u => u.UserID == userId);
                if (user == null)
                {
                    return;
                }
                switch (user.UserTypeID)
                {
                case (int)UserType.Admin:
                    user.UserTypeID = (int)UserType.User;
                    break;

                case (int)UserType.User:
                    user.UserTypeID = (int)UserType.Admin;
                    break;
                }
                ctx.Users.Attach(user);
                ctx.Entry(user).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
        public ReservationResult ReserveDevices(int deviceTypeId, int amount, int userId = -1, bool checkOnly = false)
        {
            using (var ctx = new PcPoolEntities())
            {
                var inStock = ctx.
                              DeviceInstances.
                              Count(di => di.DeviceTypeID == deviceTypeId &&
                                    di.DeviceStatusID.Value == (int)DeviceStatus.InStock);

                var reserved = GetNumberOfReservedDevices(deviceTypeId, ctx, userId);

                var availableDevices = inStock - reserved;
                var result           = new ReservationResult()
                {
                    IsPossible = availableDevices >= amount,
                    NumberOfAvailableDevice = availableDevices > 0 ? availableDevices : 0
                };

                if (!result.IsPossible || checkOnly)
                {
                    return(result);
                }
                ctx.ReservationLists.Add(new PcPoolModels.ReservationList

                {
                    UserID       = LoggedInUserData.UserId,
                    DeviceTypeID = deviceTypeId,
                    Amount       = amount,
                    StartDate    = DateTime.UtcNow,
                    EndDate      = DateTime.UtcNow.AddDays(7)
                });

                ctx.SaveChanges();
                return(result);
            }
        }
        private static bool UpdateDeviceStatus(DeviceStatus newStatus, PcPoolModels.DeviceInstance device, PcPoolEntities ctx, int userId)
        {
            if (newStatus == DeviceStatus.Loaned)
            {
                var inventoryStatProvider = new InventoryStatProvide();
                var result = inventoryStatProvider.ReserveDevices(device.DeviceTypeID, 1, userId, true);
                if (!result.IsPossible)
                {
                    return(false);
                }
                device.DeviceStatusID = (int)newStatus;

                var reservation =
                    ctx.ReservationLists.FirstOrDefault(
                        rl =>
                        rl.UserID == userId && rl.DeviceTypeID == device.DeviceTypeID &&
                        rl.EndDate > DateTime.UtcNow);
                if (reservation != null | reservation.Amount != 0)
                {
                    reservation.Amount -= 1;
                    ctx.ReservationLists.Attach(reservation);
                    ctx.Entry(reservation).State = EntityState.Modified;

                    ctx.DeviceInstances.Attach(device);
                    ctx.Entry(device).State = EntityState.Modified;
                    ctx.SaveChanges();
                }
            }
            else
            {
                device.DeviceStatusID = (int)newStatus;
                ctx.DeviceInstances.Attach(device);
                ctx.Entry(device).State = EntityState.Modified;
                ctx.SaveChanges();
            }
            return(true);
        }
        private bool UpdateDeviceStatusAndHistory(DeviceStatus newStatus, int userId, PcPoolModels.DeviceInstance device, PcPoolEntities ctx)
        {
            var status = UpdateDeviceStatus(newStatus, device, ctx, userId);

            if (status)
            {
                AddDeviceStatusHistory(ctx, device.DeviceInstanceID, newStatus, userId);
            }
            return(status);
        }
        public DeviceInstance GetItemByRfid(string rfid)
        {
            var ctx = new PcPoolEntities();

            return(GetDeviceDetails(ctx).FirstOrDefault(dd => dd.SeriaNo == rfid));
        }