Esempio n. 1
0
        public async Task <ApplicationUser> CreateAuthor(string path, RegisterAuthorModel model)
        {
            var tenanID         = _context.Tenants.Single(x => x.JournalPath == path).Id;
            var applicationUser = new ApplicationUser
            {
                Country              = model.Country,
                Email                = model.Email,
                UserName             = model.Email,
                FirstName            = model.FirstName,
                LastName             = model.LastName,
                PhoneNumber          = _maskService.RemovePhoneMasking(model.PhoneNumber),
                AffiliationNo        = model.Affiliation,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false,
                TenantId             = tenanID
            };
            var authorRoleId = _context.Roles.Single(x => x.Name == Role.Author.ToString()).Id;

            using (var tr = _context.Database.BeginTransaction())
            {
                try
                {
                    var result = await _userManager.CreateAsync(applicationUser, model.Password);

                    if (result.Succeeded)
                    {
                        _context.UserRoles.Add(new IdentityUserRole <long> {
                            RoleId = authorRoleId, UserId = applicationUser.Id
                        });
                        _context.Authors.Add(new Author {
                            User = applicationUser
                        });
                        await _context.SaveChangesAsync();

                        await tr.CommitAsync();

                        return(applicationUser);
                    }
                    return(null);
                }
                catch (Exception)
                {
                    tr.Rollback();
                    throw;
                }
            }
        }
        public Task <HttpResponseMessage> Post(RegisterAuthorModel model)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            try
            {
                _service.Register(model.Name);
                response = Request.CreateResponse(HttpStatusCode.OK, new { Title = model.Name });
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }

            var tsc = new TaskCompletionSource <HttpResponseMessage>();

            tsc.SetResult(response);
            return(tsc.Task);
        }