public async Task <IActionResult> RegisterUser(RegisterationModelForUsers registerationModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (await CheckIfUserExist(registerationModel.UserSecurity))
            {
                return(BadRequest());
            }

            registerationModel.UserInfo.UserAddress = registerationModel.Address;

            addressManager.Add(registerationModel.Address);
            dbContextUser.Add(registerationModel.UserInfo);

            string id = await AddUserToAspNetUsers(registerationModel.UserSecurity, registerationModel.UserInfo, "user");

            if (id != null)
            {
                string imagePath = UploadToCloudinary.UploadImageToCloudinary(registerationModel.UserInfo.ImagePath);

                if (imagePath != null)
                {
                    registerationModel.UserInfo.ImagePath     = imagePath;
                    registerationModel.UserInfo.AspNetUsersId = id;
                    dbContextUser.Complete();
                    return(Ok());
                }
            }
            return(BadRequest());
        }
        public async Task <IActionResult> RegisterAgent(RegisterationModelForAgents registerationModelForAgents)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (await CheckIfUserExist(registerationModelForAgents.UserSecurity))
            {
                return(BadRequest());
            }

            var JobInDb = jobManager.
                          GetFirstOrDefaultByParam(j => j.Name == registerationModelForAgents.JobInfo.Name);

            if (JobInDb == null)
            {
                return(BadRequest());
            }

            //Get Skills from Database
            IEnumerable <Skill> skillsInDb = skillManager.GetAll().Where(s => registerationModelForAgents.Skills.ToList().Select(a => a.Name).Contains(s.Name));

            if (skillsInDb == null)
            {
                return(BadRequest());
            }

            agentManager.Add(registerationModelForAgents.AgentInfo);

            registerationModelForAgents.AgentInfo.Job = JobInDb;

            string id = await AddUserToAspNetUsers(registerationModelForAgents.UserSecurity, registerationModelForAgents.AgentInfo, "worker");

            if (id != null)
            {
                registerationModelForAgents.AgentInfo.AspNetUsersId = id;

                AddAgentSkills(skillsInDb, registerationModelForAgents.AgentInfo);

                string imagePath = UploadToCloudinary.UploadImageToCloudinary(registerationModelForAgents.AgentInfo.ImagePath);

                if (imagePath != null)
                {
                    registerationModelForAgents.AgentInfo.ImagePath = imagePath;
                    dbContextUser.Complete();
                    return(Ok());
                }
            }

            return(BadRequest());
        }
Example #3
0
        public async Task <IActionResult> EditAgentProfile(string userName, EditAgent editedAgent)
        {
            if (userName == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            var userInaspNetTable = await aspUserManager.FindByNameAsync(userName);

            var agent = agentManager
                        .GetAllWithInclude(a => a.Job, a => a.AspNetUsersId == userInaspNetTable.Id).FirstOrDefault();

            if (agent.ImagePath != editedAgent.EditAgentInfo.ImagePath)
            {
                editedAgent.EditAgentInfo.ImagePath = UploadToCloudinary.UploadImageToCloudinary(editedAgent.EditAgentInfo.ImagePath);
            }

            mapper.Map(editedAgent.EditAgentInfo, agent);

            var agentEditedSkills = skillManager
                                    .GetAll()
                                    .Where(s => editedAgent.EditSkills.ToList().Select(a => a.Name).Contains(s.Name))
                                    .Select(s => s.Id)
                                    .ToList();

            var agentSkillsInDb = agentSkillManager
                                  .GetAll()
                                  .Where(s => s.AgentId == agent.Id)
                                  .ToList();


            UpdateAgentSkills(agentEditedSkills, agentSkillsInDb);


            agentManager.Complete();

            return(Ok());
        }