public async Task <PagedList <MdaDeviceAttribute> > GetDeviceAttributes(MdaDeviceAttributeQuery filter)
        {
            var query = _context.MdaDeviceAttribute
                        .Include(d => d.DeviceAttributeType)
                        .Include(d => d.Device).ThenInclude(dv => dv.Product).ThenInclude(p => p.ProductModel)
                        .AsQueryable();

            // if (filter.PageSize == 0)
            //     filter.PageSize = 10;

            if (filter.DeviceId.HasValue)
            {
                query = query.Where(a => a.DeviceId == filter.DeviceId);
            }
            if (filter.DeviceAttributeTypeId.HasValue)
            {
                query = query.Where(a => a.DeviceAttributeTypeId == filter.DeviceAttributeTypeId);
            }
            if (!string.IsNullOrEmpty(filter.Value))
            {
                query = query.Where(a => a.Value.Contains(filter.Value));
            }

            var columnsMap = new Dictionary <string, Expression <Func <MdaDeviceAttribute, object> > >
            {
            };

            query = query.ApplyOrdering(filter, columnsMap);

            // query = query.ApplyPaging(filter);

            // return await query.ToListAsync();

            return(await PagedList <MdaDeviceAttribute> .CreateAsync(query, filter.Page, filter.PageSize));
        }
예제 #2
0
        public async Task <IActionResult> AddDeviceAttribute([FromBody] DeviceAttributeSaveResource deviceAttributeSaveResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var deviceAttribute = _mapper.Map <MdaDeviceAttribute>(deviceAttributeSaveResource);

            /*  Confirm Attribute Type Does Not Exist */
            var query = new MdaDeviceAttributeQuery {
                DeviceId = deviceAttribute.DeviceId,
                DeviceAttributeTypeId = deviceAttribute.DeviceAttributeTypeId
            };
            var deviceAttributeFromRepo = await _repo.GetDeviceAttributes(query);

            if (deviceAttributeFromRepo.Any())
            {
                return(BadRequest("This attribute type already exists for the specified device"));
            }

            deviceAttribute.CreatedBy = User.Identity.Name;

            _repo.Add(deviceAttribute);

            if (await _repo.SaveAll())
            {
                return(Ok(deviceAttribute));
            }

            return(BadRequest("Failed to add device attribute"));
        }