Beispiel #1
0
        public async Task <IActionResult> RegisterUser([FromBody] UserDto userData)
        {
            // Argument verification
            if (string.IsNullOrWhiteSpace(userData.Username))
            {
                return(BadRequest(new { message = "Username is required!" }));
            }
            if (string.IsNullOrWhiteSpace(userData.Email))
            {
                return(BadRequest(new { message = "Email is required!" }));
            }
            if (string.IsNullOrWhiteSpace(userData.Password))
            {
                return(BadRequest(new { message = "Password is required!" }));
            }

            // Check if provided username is already taken
            var usernameTaken = await db.Users.AnyAsync(x => x.Username == userData.Username);

            if (usernameTaken)
            {
                return(BadRequest(new { message = "Username \"" + userData.Username + "\" is unavailable." }));
            }

            // Check if provided email address is already in use
            var emailTaken = await db.Users.AnyAsync(x => x.Email == userData.Email);

            if (emailTaken)
            {
                return(BadRequest(new { message = "Email \"" + userData.Email + "\" is already in use." }));
            }

            // If username and email are available we can create a new user
            var user = new User
            {
                Username         = userData.Username,
                Email            = userData.Email,
                RegistrationDate = DateTime.Now
            };

            user.CreatePasswordHash(userData.Password);

            // Add newly created user to the DB and save DB changes
            await db.Users.AddAsync(user);

            await db.SaveChangesAsync();

            return(Ok(new { message = "User registration successful!" }));
        }
Beispiel #2
0
        public async Task <IActionResult> DeleteAllDocuments()
        {
            // Find the currently authorized user in the database.
            // We're using eager loading to load all of the user's documents into the List< Document > attribute when we find the user
            var authId = Int32.Parse(HttpContext.User.Identity.Name);
            var user   = await db.Users.Include("Documents").Where(x => x.UserId == authId).SingleOrDefaultAsync();

            if (user == null)
            {
                return(Unauthorized(new { message = "You are not authorized to perform that action." }));
            }

            // Find all the documents owned by the currently authorized user & delete them from database
            var docs = await db.Documents.Where(x => x.OwnerId == user.UserId).ToListAsync();

            if (docs == null)
            {
                return(NotFound(new { message = "Requested documents not found." }));
            }
            db.Documents.RemoveRange(docs);
            await db.SaveChangesAsync();

            return(Ok(new { message = "All documents successfully deleted." }));
        }