public async Task <ActionResult <AdminDto> > CreateAdmin([FromBody] AdminAddDto adminAddDto) { //ApiController在遇到adminAddDto为空时可以自动返回400错误 var admin = _mapper.Map <Admin>(adminAddDto); _adminRepository.AddAdmin(admin);//只是被添加到DbContext里 await _adminRepository.SaveAsync(); var adminDto = _mapper.Map <AdminDto>(admin); return(CreatedAtRoute(nameof(GetAdmin), new { adminId = admin.AdminId }, adminDto)); }
public async Task <IActionResult> AdminLogin(AdminAddDto adminAddDto) { var adminFromRepo = await _repo.AdminLogIn(adminAddDto.Name, adminAddDto.Password); if (adminFromRepo == null) { return(Unauthorized()); } var token = GenerateToken(adminFromRepo.Name, adminFromRepo.Role.RoleName); var user = _mapper.Map <AdminForReturnDto>(adminFromRepo); return(Ok(new { token, user })); }
/// <summary> /// 添加管理员 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <ReturnData <bool> > CreateAsync(AdminAddDto model) { var result = new ReturnData <bool>(); if (await CheckAdminEmailAsync(model.Email)) { _context.Admins.Add( new Admin { Account = model.Account, Id = Guid.NewGuid().ToString(), Email = model.Email, Name = model.Name, PassWord = MD5Encrypt32(model.Password), } ); } result.Result = await _context.SaveChangesAsync() > 0; return(result); }
public async Task <IActionResult> AddAdmin(AdminAddDto admin) { if (admin == null) { return(BadRequest()); } if (await _repo.IsExistsAdmin(admin.Name)) { return(BadRequest("Admin already Exist")); } var AdminToCreate = _mapper.Map <Admins>(admin); AdminToCreate.RoleId = 2; //Role 2 means Admin var createdAdmin = await _repo.AddAdmin(AdminToCreate, admin.Password); /* var userToReturn = _mapper.Map<UserForDetailedDto>(userToCreate); * * return CreatedAtRoute("GetUser", new{Controller = "Users", id =userToCreate.Id}, userToReturn); */ return(Ok()); }
public async Task <IActionResult> Create(AdminAddDto model) { var result = await _service.CreateAsync(model); return(Ok(result)); }