示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="createUser"></param>
        /// <returns></returns>
        public CreateUserResult CreateNewUser(UserCreationDto createUser, ClaimsPrincipal claims)
        {
            CreateUserResult result = new CreateUserResult()
            {
                CreationError        = UserCreationError.NoError,
                IsCreatedSuccesfully = true
            };

            // First check if user alredy exists in database
            var users = userRepo.GetUsers(new UserQueryParameters()
            {
                UserName = createUser.UserName
            });

            if (users.Any(u => string.Compare(u.UserName, createUser.UserName, StringComparison.OrdinalIgnoreCase) == 0))
            {
                result.IsCreatedSuccesfully = false;
                result.CreationError        = UserCreationError.LoginAlreadyExists;
                return(result);
            }

            Users user = Mapper.Map <Users>(createUser);

            // Create appropriate user data according to flags
            if (createUser.IsTeacher)
            {
                Teachers teacher = Mapper.Map <Teachers>(createUser);
                teacherRepo.AddTeacher(teacher, claims);
                teacherRepo.Save();
                user.TeacherId     = teacher.TeacherId;
                result.CreatedUser = Mapper.Map <UserDto>(teacher);
            }

            if (createUser.IsStudent)
            {
                Students student = Mapper.Map <Students>(createUser);
                studentRepo.AddStudent(student, claims);
                studentRepo.Save();
                user.StudentId     = student.StudentId;
                result.CreatedUser = Mapper.Map <UserDto>(student);
            }

            if (createUser.IsParent)
            {
                Parents parent = Mapper.Map <Parents>(createUser);
                parentRepo.AddParent(parent, claims);
                parentRepo.Save();
                user.ParentId      = parent.ParentId;
                result.CreatedUser = Mapper.Map <UserDto>(parent);
            }

            userRepo.CreateUser(user, claims);
            userRepo.Save();
            result.CreatedUser.UserId = user.UserId.ToString();

            return(result);
        }
示例#2
0
        public IActionResult CreateClass([FromBody] ParentCreationDto parent)
        {
            Parents pr = Mapper.Map <Parents>(parent);

            parentRepo.AddParent(pr, User);

            parentRepo.Save();
            return(CreatedAtRoute("GetParent", new { id = pr.ParentId }, pr));
        }
        public async Task <ActionResult> AddParent([FromForm] Parent parent, IFormFile file)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (parent == null)
            {
                return(NotFound());
            }
            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }
            if (file.Length > _photoSetting.MaxBytes)
            {
                return(BadRequest("Max file size exceeded"));
            }
            if (!_photoSetting.IsSupported(file.FileName))
            {
                return(BadRequest("Invalid file type"));
            }
            var uploadsFolderPath = Path.Combine(_host.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }
            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);  // filepath

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream); // picture saved to the path (folder)
            }

            parent.Picture = fileName;
            await _parentRepository.AddParent(parent);

            return(Created("Parent Table", parent));
        }