public async Task <bool?> AddRequest(ApplicationDbContext db, string userId, StatusInCompany status) { if (status != StatusInCompany.RequestedByCompany && status != StatusInCompany.RequestedByUser) { return(false); } if (string.IsNullOrWhiteSpace(userId)) { return(false); } //var userRelation = await db.CompanyUsers.FirstOrDefaultAsync(x1 => x1.UserId == userId); var userRelation = await db.Entry(this).Collection(x1 => x1.CompanyUsers).Query().FirstOrDefaultAsync(x1 => x1.UserId == userId); if (userRelation?.Status == StatusInCompany.Empty) { if (!await userRelation.ChangeStatus(db, status)) { return(null); } return(true); } if (userRelation == null) { db.CompanyUsers.Add(new CompanyUser(userId, this.Id, status)); await db.SaveChangesAsync(); return(true); } return(false); }
public async Task <bool?> RemoveUser(ApplicationDbContext db, string userId, StatusInCompany status) { if (status == StatusInCompany.Moderator) { var countModer = await db.Entry(this).Collection(x1 => x1.CompanyUsers).Query().Where(x1 => x1.Status == status).CountAsync(); if (countModer < 2) { return(null); } } var userRelation = await db.Entry(this).Collection(x1 => x1.CompanyUsers).Query().FirstOrDefaultAsync(x1 => x1.UserId == userId && x1.Status == status); if (userRelation == null) { return(false); } //db.CompanyUsers.Remove(userRelation); if (!await userRelation.ChangeStatus(db, StatusInCompany.Empty)) { return(null); } return(true); }
public async Task <bool> ChangeStatus(ApplicationDbContext db, StatusInCompany newStatus) { try { this.Status = newStatus; await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { return(false); } return(true); }
//создает если надо и изменяет запись .(если записи нет то создаст ее.) у записи будет переданный статус, нет валидации!!! //null-если ошибка public async Task <bool?> CreateChangeStatusUser(ApplicationDbContext db, StatusInCompany newStatus, string userId) { bool exists = false; var record = await db.Entry(this).Collection(x1 => x1.CompanyUsers).Query().FirstOrDefaultAsync(x1 => x1.UserId == userId); if (record == null) { db.CompanyUsers.Add(new CompanyUser(userId, this.Id, newStatus)); await db.SaveChangesAsync(); exists = false; } else { exists = true; if (!await record.ChangeStatus(db, newStatus)) { return(null); } } return(exists); }
public async Task <bool?> DeleteUserFromCompany([FromForm] int companyId, [FromForm] string newUserId, StatusInCompany status) { string userId = AuthJWT.GetCurrentId(HttpContext, out int statusId); if (statusId != 0 || userId == null) { Response.StatusCode = 401; return(null); } var company = await Company.GetIfAccess(_db, userId, companyId); if (company == null) { Response.StatusCode = 404; return(null); } var res = await company.RemoveUser(_db, newUserId, status);// StatusInCompany.Moderator); if (res == null) { Response.StatusCode = 527; return(null); } return(res); }
public async Task <bool?> AddUserToCompany([FromForm] int companyId, [FromForm] string newUserId, [FromForm] StatusInCompany newStatus) { string userId = AuthJWT.GetCurrentId(HttpContext, out int statusId); if (statusId != 0 || userId == null) { Response.StatusCode = 401; return(null); } var company = await Company.GetIfAccess(_db, userId, companyId); if (company == null) { Response.StatusCode = 404; return(null); } bool?res = null; if (newStatus == StatusInCompany.Moderator) { res = await company.AddHeadUser(_db, newUserId); } if (newStatus == StatusInCompany.Employee) { //#TODO //res = await company.AddHeadUser(_db, newUserId); } if (res == null) { Response.StatusCode = 527; return(null); } return(res); }
public CompanyUser(string userId, int companyId, StatusInCompany status) : this(userId, companyId) { Status = status; }