Beispiel #1
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.Id)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetUser", new { id = user.Id }, user));
        }
Beispiel #2
0
        public async Task <ActionResult <IEnumerable <Course> > > Enroll(int userId, int courseId)
        {
            if (await _context.Courses.FindAsync(courseId) == null)
            {
                return(BadRequest(new { message = $"Course with Id: {courseId} does not exist!" }));
            }

            if (await _context.Users.FindAsync(userId) == null)
            {
                return(BadRequest(new { message = $"User with Id: {userId} does not exist!" }));
            }

            if (await _context.Enrollments.FindAsync(userId, courseId) != null)
            {
                return(BadRequest(new { message = "User already enrolled to that course!" }));
            }

            await _context.Enrollments.AddAsync(new Enrollment { UserId = userId, CourseId = courseId });

            await _context.SaveChangesAsync();

            return(Ok(new { message = $"User: {userId} successfully enrolled to course: {courseId}!" }));
        }