예제 #1
0
        public async Task <IActionResult> AddUser([FromForm] ProjectUserVM formdata)
        {
            // Find Tag In Database
            var projectUser = _dbContext.ProjectUsers.Find(formdata.UserID, formdata.ProjectID);

            // Update Project User If Exist
            if (projectUser != null)
            {
                // If Project User Is Not Follower Return Error
                if (projectUser.UserRole != "follower")
                {
                    return(Conflict(new { result = formdata, message = "Project User Already Exist" }));
                }

                // Add Tag To Project
                projectUser.UserRole = formdata.UserRole;

                await _dbContext.SaveChangesAsync();

                _dbContext.Entry(projectUser).Reference(pu => pu.User).Load();

                // Return Ok Status
                return(Ok(new
                {
                    result = projectUser,
                    message = "Project User Successfully Updated"
                }));
            }

            // Create Many To Many Connection
            projectUser = new ProjectUser
            {
                ProjectID   = formdata.ProjectID,
                UserID      = formdata.UserID,
                UserRole    = formdata.UserRole,
                IsFollowing = formdata.IsFollowing
            };

            // Add To Database And Save Change
            await _dbContext.ProjectUsers.AddAsync(projectUser);

            await _dbContext.SaveChangesAsync();

            _dbContext.Entry(projectUser).Reference(pu => pu.User).Load();

            // Return Ok Status
            return(Ok(new
            {
                result = projectUser,
                message = "Project User Successfully Created"
            }));
        }
예제 #2
0
        public async Task <IActionResult> UpdateUser([FromForm] ProjectUserVM formdata)
        {
            // Find Many To Many
            var userRole = await _dbContext.ProjectUsers.FindAsync(formdata.UserID, formdata.ProjectID);

            if (userRole == null)
            {
                return(NotFound(new { message = "User Not Found" }));
            }

            //Remove Follower If Not Following
            if (formdata.UserRole == "follower" && formdata.IsFollowing == false)
            {
                // Remove User Role
                _dbContext.ProjectUsers.Remove(userRole);

                // Save Change
                await _dbContext.SaveChangesAsync();

                return(Ok(new
                {
                    result = userRole,
                    message = "Follower successfully deleted"
                }));
            }

            // Update Role
            userRole.UserRole    = formdata.UserRole;
            userRole.IsFollowing = formdata.IsFollowing;

            // Set Entity State
            _dbContext.Entry(userRole).State = EntityState.Modified;

            // Save Change
            await _dbContext.SaveChangesAsync();

            // Return Ok Status
            return(Ok(new
            {
                result = userRole,
                message = "Project successfully updated."
            }));
        }