Ejemplo n.º 1
0
        //Save Tasks to the database to the Tasks table
        public static void SaveTaskForLoggedInUser(TaskViewModel taskViewModel, string userId)
        {
            var regionName        = Denmark_addressess.GetMunicipalityCode(taskViewModel.ClientStreetName, taskViewModel.ClientPostCode.ToString(), taskViewModel.ClientHouseNumber, taskViewModel.ClientCity);
            MitBudDBEntities db   = new MitBudDBEntities();
            Task             Task = new Task();

            Task.Title             = taskViewModel.Title;
            Task.Client_id         = userId;
            Task.Description       = taskViewModel.Description;
            Task.Category          = taskViewModel.Category;
            Task.ClientName        = taskViewModel.ClientName;
            Task.ClientStreetName  = taskViewModel.ClientStreetName;
            Task.ClientHouseNumber = taskViewModel.ClientHouseNumber;
            Task.ClientPostCode    = taskViewModel.ClientPostCode;
            Task.Region            = regionName;
            Task.ClientCity        = taskViewModel.ClientCity;
            Task.ClientTelephone   = taskViewModel.ClientTelephone;
            Task.ClientEmail       = taskViewModel.ClientEmail;
            Task.WhoAreYou         = taskViewModel.WhoAreYou;
            Task.TaskCost          = taskViewModel.TaskCost;
            Task.Date        = taskViewModel.Date;
            Task.DesiredDate = taskViewModel.DesiredDate;
            Task.Image       = taskViewModel.Image;
            Task.Status      = taskViewModel.Status;
            db.Tasks.Add(Task);
            db.SaveChanges();
        }
        public async Task <IHttpActionResult> NotRegisteredUserTask(TaskViewModel taskViewModel)
        {
            taskViewModel.Region = Denmark_addressess.GetMunicipalityCode(taskViewModel.ClientStreetName, taskViewModel.ClientPostCode.ToString(), taskViewModel.ClientHouseNumber, taskViewModel.ClientCity);


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var            randomPass     = GenerateRandomPassword.GenerateRandomPass();
            RegisterClient registerClient = new RegisterClient();

            registerClient.Email           = taskViewModel.ClientEmail;
            registerClient.Password        = randomPass;
            registerClient.ConfirmPassword = randomPass;

            var user = new ApplicationUser()
            {
                UserName = taskViewModel.ClientEmail, Email = taskViewModel.ClientEmail
            };

            IdentityResult result = await UserManager.CreateAsync(user, randomPass);

            if (result.Succeeded)
            {
                var UserId = UserManager.FindByEmail(taskViewModel.ClientEmail);

                ClientProvider.SaveClientInfo(taskViewModel.ClientName, taskViewModel.ClientEmail, UserId.Id);

                TaskProvider.SaveTaskRegisteredUser(taskViewModel, UserId.Id);
                UserManager.AddToRole(UserId.Id, "Client");
            }


            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }
            var httpStatusCode = HttpStatusCode.Accepted;
            var responseMsg    = new HttpResponseMessage(httpStatusCode)
            {
                Content = new StringContent("", Encoding.UTF8, "application/json")
            };

            MitBudDBEntities mitBudDB = new MitBudDBEntities();


            var UserEmail = mitBudDB.AspNetUsers.Where(x => x.Email == taskViewModel.ClientEmail).SingleOrDefault();

            string token = await UserManager.GeneratePasswordResetTokenAsync(UserEmail.Id);

            Email.sendCreatePasswordByEmail(taskViewModel.ClientEmail, taskViewModel.ClientName, token);

            return(Ok());
        }