Beispiel #1
0
        public Asset CreateAsset(Asset asset)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var current = AssetDao.ReadFromFile().ToList();

            if (asset.Id == "")
            {
                var id = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                id       = id.Replace('/', '_');
                id       = id.Replace('+', '-');
                asset.Id = id;
            }
            else
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);  //ID has to be auto assigned
            }
            current.Add(asset);

            AssetDao.WriteToFile(current);

            return(asset);
        }
Beispiel #2
0
        public IEnumerable <Asset> GetAsset(Group group)
        {
            var asset = AssetDao.ReadFromFile().Where(a => a.Group == group);

            if (asset == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return(asset);
        }
Beispiel #3
0
        // GET /api/asset/1
        public Asset GetAsset(string id)
        {
            var asset = AssetDao.ReadFromFile().SingleOrDefault(c => Equals(c.Id, id));

            if (asset == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return(asset);
        }
Beispiel #4
0
        public IHttpActionResult DeleteAsset(string id)
        {
            var list        = AssetDao.ReadFromFile().ToList();
            var assetInFile = list.SingleOrDefault(c => Equals(c.Id, id));

            if (assetInFile == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            list.Remove(assetInFile);

            AssetDao.WriteToFile(list);
            return(Ok());
        }
Beispiel #5
0
        public IHttpActionResult UpdateAssetCount(AssetPatchRequest request)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var list        = AssetDao.ReadFromFile().ToList();
            var assetInFile = list.SingleOrDefault(c => Equals(c.Id, request.Id));

            if (assetInFile == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            list.Remove(assetInFile);
            assetInFile.Count = request.Count;
            list.Add(assetInFile);
            AssetDao.WriteToFile(list);
            return(Ok());
        }
        // GET: Asset
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            ViewBag.CurrentSort = sortOrder;

            ViewBag.NameSortParm       = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.GroupSortParm      = sortOrder == "group_asc" ? "group_desc" : "group_asc";
            ViewBag.PriceSortParm      = sortOrder == "price_asc" ? "price_desc" : "price_asc";
            ViewBag.CountSortParm      = sortOrder == "count_asc" ? "count_desc" : "count_asc";
            ViewBag.TotalPriceSortParm = sortOrder == "total_asc" ? "total_desc" : "total_asc";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var list = AssetDao.ReadFromFile();

            // Search box filtering
            if (!String.IsNullOrEmpty(searchString))
            {
                list = list.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper()) ||
                                  s.Group.ToString().ToUpper().Contains(searchString.ToUpper()) ||
                                  s.Id.ToUpper().Contains(searchString));
            }

            // Sorting
            switch (sortOrder)
            {
            case "price_asc":
                list = list.OrderBy(s => s.UnitPrice);
                break;

            case "price_desc":
                list = list.OrderByDescending(s => s.UnitPrice);
                break;

            case "count_asc":
                list = list.OrderBy(s => s.Count);
                break;

            case "count_desc":
                list = list.OrderByDescending(s => s.Count);
                break;

            case "total_asc":
                list = list.OrderBy(s => s.TotalValue);
                break;

            case "total_desc":
                list = list.OrderByDescending(s => s.TotalValue);
                break;

            case "group_asc":
                list = list.OrderBy(s => s.Group);
                break;

            case "group_desc":
                list = list.OrderByDescending(s => s.Group);
                break;

            case "name_desc":
                list = list.OrderByDescending(s => s.Name);
                break;

            default:
                list = list.OrderBy(s => s.Name);
                break;
            }

            int pageSize   = 10;
            int pageNumber = (page ?? 1);

            return(View(list.ToPagedList(pageNumber, pageSize)));
        }
Beispiel #7
0
 // GET /api/asset
 public IEnumerable <Asset> GetAsset()
 {
     return(AssetDao.ReadFromFile());
 }