public bool Create(GroupCreateApiModel apiModel) { var validator = _groupValidator.Validate(apiModel); if (validator.IsValid) { return(_groupRepository.Create(apiModel)); } return(false); }
public bool Create(GroupCreateApiModel g) { var newGroup = new GROUP(); try { newGroup.Name = g.name; foreach (var p in g.permissions) { var perm = db.PERMISSIONs.Find(p); newGroup.PERMISSIONs.Add(perm); } db.GROUPs.Add(newGroup); db.SaveChanges(); return(true); } catch { throw; } }
public bool Update(int id, GroupCreateApiModel g) { var dbGroup = db.GROUPs.Find(id); if (dbGroup != null) { dbGroup.Name = g.name; dbGroup.PERMISSIONs.Clear(); foreach (var p in g.permissions) { var perm = db.PERMISSIONs.Find(p); dbGroup.PERMISSIONs.Add(perm); } db.SaveChanges(); return(true); } else { return(false); } }
public HttpResponseMessage Update([FromUri] int id, [FromBody] GroupCreateApiModel apiModel) { var response = new HttpResponseMessage(); ResponseFormat responseData = new ResponseFormat(); AuthorizationService _authorizationService = new AuthorizationService().SetPerm((int)EnumPermissions.GROUP_MODIFY); //read jwt IEnumerable <string> headerValues; if (Request.Headers.TryGetValues("Authorization", out headerValues)) { string jwt = headerValues.FirstOrDefault(); //validate jwt var payload = JwtTokenManager.ValidateJwtToken(jwt); if (payload.ContainsKey("error")) { if ((string)payload["error"] == ErrorMessages.TOKEN_EXPIRED) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_EXPIRED; } if ((string)payload["error"] == ErrorMessages.TOKEN_INVALID) { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.TOKEN_INVALID; } } else { var userId = payload["id"]; var isAuthorized = _authorizationService.Authorize(Convert.ToInt32(userId)); if (isAuthorized) { var isUpdated = _groupService.Update(id, apiModel); if (isUpdated) { response.StatusCode = HttpStatusCode.OK; responseData = ResponseFormat.Success; responseData.message = SuccessMessages.GROUP_MODIFIED; } } else { response.StatusCode = HttpStatusCode.Forbidden; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } } } else { response.StatusCode = HttpStatusCode.Unauthorized; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.UNAUTHORIZED; } var json = JsonConvert.SerializeObject(responseData); response.Content = new StringContent(json, Encoding.UTF8, "application/json"); return(response); }