public async Task <BaseResponse <UserResponse> > Add(UserRequest userRequest) { var response = new BaseResponse <UserResponse>(); var results = _validator.Validate(userRequest); if (!results.IsValid) { results.Errors.ToList().ForEach(x => { response.AddErrors(x.ErrorMessage, HttpStatusCode.BadRequest); }); return(response); } var user = _mapper.Map <Model.Entities.User>(userRequest); await _context.Users.AddAsync(user); var save = await SaveChanges(); if (!save.Item1) { response.AddErrors(save.Item2, HttpStatusCode.InternalServerError); return(response); } var userResponse = _mapper.Map <UserResponse>(user); response.AddItems(new[] { userResponse }); return(response); }
public async Task <BaseResponse <UserResponse> > GetByHashId(string hashId) { var response = new BaseResponse <UserResponse>(); var user = _context.Users.FirstOrDefault(x => x.HashId == hashId); response.AddItems(user == null ? null : new[] { _mapper.Map <UserResponse>(user) }); return(await Task.FromResult(response)); }
public async Task <BaseResponse <UserResponse> > GetAll() { var response = new BaseResponse <UserResponse>(); var users = await _context.Users.ToArrayAsync(); response.AddItems(_mapper.Map <UserResponse[]>(users)); return(response); }
public async Task <BaseResponse <UserResponse> > UpdateByHashId(string hashId, UserRequest userRequest) { var response = new BaseResponse <UserResponse>(); var user = _context.Users.FirstOrDefault(x => x.HashId == hashId); if (user == null) { response.AddItems(null); return(response); } var results = _validator.Validate(userRequest); if (!results.IsValid) { results.Errors.ToList().ForEach(x => { response.AddErrors(x.ErrorMessage, HttpStatusCode.BadRequest); }); return(response); } user.Name = userRequest.Name; user.Age = userRequest.Age; user.Address = userRequest.Address; user.UpdateDate = DateTime.Now; var save = await SaveChanges(); if (!save.Item1) { response.AddErrors(save.Item2, HttpStatusCode.InternalServerError); return(response); } var userResponse = _mapper.Map <UserResponse>(user); response.AddItems(new[] { userResponse }); return(response); }