예제 #1
0
        internal async void LoadUsers()
        {
            var response = await mainWindow.CommandQueryDispatcher.SendAsync <IEnumerable <UserListItem> >($"api/User-management/Users");

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                MessageBox.Show("Failed to load Users");
            }
            else
            {
                Users = response.ResponseContent.Select(x => UserCollectionItem.FromContract(x)).ToList();
                UserDataGrid.ItemsSource = Users;
            }
        }
예제 #2
0
        public async Task TestCase_CURD(string usr)
        {
            var context = fixture.GetCurrentDataContext();
            UserCollectionsController control = new UserCollectionsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(usr);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userclaim
                }
            };

            // Step 1. Get all
            var getallrst = control.Get();

            Assert.NotNull(getallrst);
            var collindb = context.UserCollections.AsNoTracking().Where(p => p.User == usr).ToList();

            Assert.Equal(collindb.Count, getallrst.Count());

            // Step 2. Create one
            var createdone = new UserCollection()
            {
                Name    = "Test 1" + usr,
                Comment = "Test",
                User    = usr,
            };
            var createditem = new UserCollectionItem()
            {
                RefType = TagRefType.KnowledgeItem,
                RefID   = DataSetupUtility.Knowledge1ID
            };

            createdone.Items.Add(createditem);
            var postrst = await control.Post(createdone);

            Assert.NotNull(postrst);
            var postcreatedrst = Assert.IsType <CreatedODataResult <UserCollection> >(postrst);
            var crtid          = postcreatedrst.Entity.ID;

            objectsCreated.Add(crtid);
            Assert.Equal(createdone.User, postcreatedrst.Entity.User);
            Assert.Equal(createdone.Name, postcreatedrst.Entity.Name);
            Assert.Equal(createdone.Comment, postcreatedrst.Entity.Comment);
            var collitemindb = context.UserCollectionItems.AsNoTracking().Where(p => p.ID == crtid).ToList();

            Assert.Single(collitemindb);
            Assert.Equal(createditem.RefType, collitemindb[0].RefType);
            Assert.Equal(createditem.RefID, collitemindb[0].RefID);

            // Step 3. Get single
            var getsinglerst = control.Get(crtid);

            Assert.NotNull(getsinglerst);
            var getrstresult = Assert.IsType <SingleResult <UserCollection> >(getsinglerst);
            var readitem     = getrstresult.Queryable.ToList().ElementAtOrDefault(0);

            Assert.NotNull(readitem);
            Assert.Equal(crtid, readitem.ID);
            Assert.Equal(createdone.User, readitem.User);
            Assert.Equal(createdone.Name, readitem.Name);
            Assert.Equal(createdone.Comment, readitem.Comment);

            // Step 4. Change via PUT
            readitem.Comment += "Changed";
            readitem.Items.Clear();
            createditem = new UserCollectionItem()
            {
                RefID      = DataSetupUtility.Exercise1ID,
                RefType    = TagRefType.ExerciseItem,
                ID         = crtid,
                Collection = readitem,
            };
            readitem.Items.Add(createditem);
            var putrst = await control.Put(crtid, readitem);

            Assert.NotNull(putrst);
            var putokrst = Assert.IsType <OkObjectResult>(putrst);

            Assert.NotNull(putokrst);
            var putokrstobj = Assert.IsType <UserCollection>(putokrst.Value);

            Assert.Equal(readitem.ID, putokrstobj.ID);
            Assert.Equal(readitem.User, putokrstobj.User);
            Assert.Equal(readitem.Name, putokrstobj.Name);
            Assert.Equal(readitem.Comment, putokrstobj.Comment);
            collindb     = context.UserCollections.AsNoTracking().Where(p => p.User == usr).ToList();
            collitemindb = context.UserCollectionItems.AsNoTracking().Where(p => p.ID == crtid).ToList();
            Assert.Single(collindb);
            Assert.Single(collitemindb);
            Assert.Equal(readitem.Comment, collindb[0].Comment);
            Assert.Equal(createditem.RefID, collitemindb[0].RefID);
            Assert.Equal(createditem.RefType, collitemindb[0].RefType);

            // Step 5. Delete
            var delrst = await control.Delete(crtid);

            Assert.NotNull(delrst);
            var delrtn = Assert.IsType <StatusCodeResult>(delrst);

            objectsCreated.Remove(crtid);

            await context.DisposeAsync();
        }
        public async Task <IActionResult> AddItemToCollection([FromBody] ODataActionParameters parameters)
        {
            if (!ModelState.IsValid)
            {
                foreach (var value in ModelState.Values)
                {
                    foreach (var err in value.Errors)
                    {
                        System.Diagnostics.Debug.WriteLine(err.Exception?.Message);
                    }
                }

                return(BadRequest(ModelState));
            }

            String user  = (String)parameters["User"];
            String usrId = ControllerUtil.GetUserID(this);

            if (String.IsNullOrEmpty(usrId) || user != usrId)
            {
                throw new Exception("Failed ID");
            }
            int        collid        = (int)parameters["ID"];
            int        refid         = (int)parameters["RefID"];
            TagRefType reftype       = (TagRefType)parameters["RefType"];
            var        createAtValue = parameters["CreatedAt"];
            DateTime?  createdAt     = null;

            if (createAtValue != null)
            {
                createdAt = ((DateTimeOffset)createAtValue).DateTime;
            }
            else
            {
                createdAt = DateTime.Today;
            }
            if (String.IsNullOrEmpty(user) || refid <= 0)
            {
                return(BadRequest("Invalid input"));
            }

            // Check collection header
            var collcnt = (from collheader in this._context.UserCollections
                           where collheader.ID == collid && collheader.User == user select collheader.ID).Count();

            if (collcnt != 1)
            {
                return(BadRequest("Invalid collection"));
            }

            // Check existence of item
            var itemcnt = (from collitem in this._context.UserCollectionItems
                           where collitem.RefType == reftype && collitem.RefID == refid && collitem.ID == collid
                           select collitem.ID).Count();

            if (itemcnt > 0)
            {
                return(NoContent());
            }

            // Check existence of ref. id
            switch (reftype)
            {
            case TagRefType.KnowledgeItem:
                break;

            case TagRefType.ExerciseItem:
            default:
            {
                var refcnt = (from exec in _context.ExerciseItems
                              where exec.ID == refid
                              select exec.ID).Count();
                if (refcnt != 1)
                {
                    return(BadRequest("Invalid refence ID"));
                }
            }
            break;
            }

            var nitem = new UserCollectionItem();

            nitem.ID        = collid;
            nitem.RefID     = refid;
            nitem.RefType   = reftype;
            nitem.CreatedAt = createdAt;
            this._context.UserCollectionItems.Add(nitem);
            await this._context.SaveChangesAsync();

            return(Ok(nitem));
        }
        public async Task <IActionResult> AddItemToCollectionEx([FromBody] ODataActionParameters parameters)
        {
            if (!ModelState.IsValid)
            {
                foreach (var value in ModelState.Values)
                {
                    foreach (var err in value.Errors)
                    {
                        System.Diagnostics.Debug.WriteLine(err.Exception?.Message);
                    }
                }

                return(BadRequest(ModelState));
            }

            String user = (String)parameters["User"];

            if (String.IsNullOrEmpty(user))
            {
                return(BadRequest("Invalid input"));
            }
            String usrId = ControllerUtil.GetUserID(this);

            if (String.IsNullOrEmpty(usrId) || user != usrId)
            {
                throw new Exception("Failed ID");
            }
            var items = (IEnumerable <UserCollectionItem>)parameters["UserCollectionItems"];

            foreach (var item in items)
            {
                if (item != null)
                {
                    if (item.CreatedAt == null)
                    {
                        item.CreatedAt = DateTime.Today;
                    }

                    // Check collection header
                    var collcnt = (from collheader in this._context.UserCollections
                                   where collheader.ID == item.ID && collheader.User == user
                                   select collheader.ID).Count();
                    if (collcnt != 1)
                    {
                        return(BadRequest("Invalid collection"));
                    }

                    // Check existence of item
                    var itemcnt = (from collitem in this._context.UserCollectionItems
                                   where collitem.RefType == item.RefType && collitem.RefID == item.RefID && collitem.ID == item.ID
                                   select collitem.ID).Count();
                    if (itemcnt > 0)
                    {
                        return(NoContent());
                    }

                    // Check existence of ref. id
                    switch (item.RefType)
                    {
                    case TagRefType.KnowledgeItem:
                        break;

                    case TagRefType.ExerciseItem:
                    default:
                    {
                        var refcnt = (from exec in _context.ExerciseItems
                                      where exec.ID == item.RefID
                                      select exec.ID).Count();
                        if (refcnt != 1)
                        {
                            return(BadRequest("Invalid refence ID"));
                        }
                    }
                    break;
                    }

                    var nitem = new UserCollectionItem(item);
                    this._context.UserCollectionItems.Add(nitem);
                }
            }
            await this._context.SaveChangesAsync();

            return(Ok(items));
        }