示例#1
0
        public async Task <IActionResult> PostInstitution(cleanData cleanData)
        {
            var institution = new Institution();
            var instance    = new Instance()
            {
                Year = cleanData.instanceYear, Semester = (Semester)cleanData.instanceSemester
            };

            institution.Name        = cleanData.name;
            institution.Abbrivation = cleanData.abbrivation;
            institution.Instance    = instance;
            string username = cleanData.username;


            var applicationUser = _context.Users.FirstOrDefault(x => x.UserName == username);

            if (applicationUser != null)
            {
                applicationUser.Status      = Status.VALIDATED;
                institution.ApplicationUser = applicationUser;
                institution.UserId          = applicationUser.Id;

                _context.Institutions.Add(institution);
                await _context.SaveChangesAsync();

                var ins = _context.Institutions.FirstOrDefault(i => i.UserId == applicationUser.Id);
                applicationUser.Institution = ins.InstitutionId;
                _context.Entry <ApplicationUser>(applicationUser).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetInstitution", new { id = institution.InstitutionId }, institution));
            }
            return(BadRequest(ModelState));
        }
示例#2
0
        public async Task <IActionResult> PostCollege(cleanData cleanData)
        {
            var             college = new College();
            ApplicationUser user    = _context.Users.FirstOrDefault(x => x.UserName == cleanData.username);

            if (user != null)
            {
                college.ApplicationUser   = user;
                college.ApplicationUserId = user.Id;
                user.Status  = Status.VALIDATED;
                college.Band = _context.Bands.FirstOrDefault(x => x.BandId == cleanData.band);

                college.Name = cleanData.name;
                ApplicationUser loggedInUser = await _userManager.GetUserAsync(HttpContext.User);

                if (loggedInUser != null)
                {
                    Institution institution = _context.Institutions.FirstOrDefault(x => x.ApplicationUser.Id == loggedInUser.Id);
                    if (institution != null)
                    {
                        college.Institution   = institution;
                        college.InstitutionId = institution.InstitutionId;
                        _context.College.Add(college);
                        await _context.SaveChangesAsync();

                        user.College = college.CollegeId;
                        _context.Entry <ApplicationUser>(user).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        return(Ok(college));
                    }
                }
            }
            return(BadRequest(ModelState));
        }