public IActionResult RemoveUserFromGroup(RemoveUserFromGroupViewModel model) { try { var domain = Service <GroupDomain>(); var groupUser = domain.GroupUsers.Id(model.id); if (groupUser == null) { return(NotFound(new ApiResult { Code = ResultCode.NotFound, Data = null, Message = ResultCode.NotFound.DisplayName() })); } var iDomain = _uow.GetService <IdentityDomain>(); var tokens = iDomain.GetTokensNameStartsWith(groupUser.User, "Firebase", "FCMToken"); var entity = domain.RemoveUserFromGroup(groupUser); var ev = _eDomain.RemoveUserFromGroup(groupUser, User); _uow.SaveChanges(); var data = new Dictionary <string, string>(); data["title"] = "A group member has been removed"; data["message"] = ev.Message; _eDomain.Notify(new Message { Topic = $"GROUP_MEMBER_{groupUser.GroupId}", Data = data }); var userData = new Dictionary <string, string>(); userData["title"] = "You've been removed from group"; userData["message"] = ev.Message; _eDomain.Notify(new Message { Topic = groupUser.UserId, Data = userData }); _eDomain.UnsubscribeFromTopic(tokens, $"GROUP_MEMBER_{groupUser.GroupId}"); _logger.CustomProperties(new { model }).Info("Remove user from group"); return(Ok(entity.Id)); } catch (Exception e) { if (e.InnerException != null && e.InnerException.GetType() == typeof(SqlException)) { return(BadRequest(new ApiResult() { Code = ResultCode.FailValidation, Data = null, Message = "Can not delete because it has dependencies" })); } _logger.Error(e); return(Error(new ApiResult() { Code = ResultCode.UnknownError, Message = ResultCode.UnknownError.DisplayName() + ": " + e.Message })); } }
public async Task <IActionResult> Delete(string id) { try { var user = await _iDomain.GetUserById(id); if (user == null) { return(NotFound(new ApiResult() { Code = ResultCode.NotFound, Data = null, Message = ResultCode.NotFound.DisplayName() })); } var mess = ""; var roles = await _iDomain.GetRoles(user); if (roles.Contains("Admin")) { mess += "Can not remove admin"; } if (!string.IsNullOrEmpty(mess)) { return(BadRequest(new ApiResult() { Code = ResultCode.FailValidation, Message = mess, Data = null })); } var ev = _eDomain.DeleteUser(user, User); var tokens = _iDomain.GetTokensNameStartsWith(user, "Firebase", "FCMToken"); var result = await _iDomain.Remove(user); if (result.Succeeded) { _uow.SaveChanges(); var data = new Dictionary <string, string>(); data["title"] = "So sorry"; data["message"] = ev.Message; _eDomain.Notify(new Message { Topic = user.Id, Data = data }); _eDomain.UnsubscribeFromTopic(tokens, user.Id); return(NoContent()); } foreach (var err in result.Errors) { ModelState.AddModelError(err.Code, err.Description); } return(BadRequest(new ApiResult() { Code = ResultCode.FailValidation, Data = ModelState, Message = ResultCode.FailValidation.DisplayName() })); } catch (Exception e) { if (e.InnerException != null && e.InnerException.GetType() == typeof(SqlException)) { return(BadRequest(new ApiResult() { Code = ResultCode.FailValidation, Data = null, Message = "Can not delete because it has dependencies" })); } _logger.Error(e); return(Error(new ApiResult() { Code = ResultCode.UnknownError, Message = ResultCode.UnknownError.DisplayName() + ": " + e.Message })); } }