public async Task UpdateSocietyAsync(string currentUserId, string id, SocietyUpdateModel societyUpdateModel) { var society = await GetSocietyAsync(id); if (society == null) { throw new Exception("The society doesn't exist"); } if (society.ManagerId == currentUserId) { throw new UnauthorizedAccessException("You are not authorize to update this society"); } var update = Builders <Society> .Update .Set(dbSociety => dbSociety.SocietyName, societyUpdateModel.SocietyName) .Set(dbSociety => dbSociety.ManagerId, societyUpdateModel.ManagerId) .Set(dbSociety => dbSociety.EmployeeNumber, societyUpdateModel.EmployeeNumber) .Set(dbSociety => dbSociety.Adress, societyUpdateModel.Adress) .Set(dbSociety => dbSociety.Town, societyUpdateModel.Town) .Set(dbSociety => dbSociety.Zipcode, societyUpdateModel.Zipcode) .Set(dbSociety => dbSociety.Country, societyUpdateModel.Country) .Set(dbSociety => dbSociety.Region, societyUpdateModel.Region); await _societiesService.UpdateOneAsync(dbSociety => dbSociety.Id == id, update ); }
public async Task <IActionResult> UpdateSociety(string id, [FromBody] SocietyUpdateModel societyUpdateModel) { var currentUserId = User.Identity.Name; try { await _societiesService.UpdateSocietyAsync(currentUserId, id, societyUpdateModel); return(Ok()); } catch (UnauthorizedAccessException e) { return(Forbid("You can't edit that society : you are not the manager of society")); } catch (Exception e) { return(BadRequest($"Can't update the society: {e.Message}")); } }