public bool Update(int id, ContactCreateApiModel apiModel, int modifiedUser) { var validator = _contactValidator.Validate(apiModel); if (validator.IsValid) { return(_contactRepository.Update(id, apiModel, modifiedUser)); } return(false); }
public bool Create(ContactCreateApiModel apiModel, int createdUser) { var validator = _contactValidator.Validate(apiModel); if (validator.IsValid) { return(_contactRepository.Create(apiModel, createdUser)); } return(false); }
public HttpResponseMessage Create(ContactCreateApiModel apiModel) { var response = new HttpResponseMessage(); ResponseFormat responseData = new ResponseFormat(); AuthorizationService _authorizationService = new AuthorizationService().SetPerm((int)EnumPermissions.CONTACT_CREATE); //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 isCreated = _contactService.Create(apiModel, Convert.ToInt32(userId)); if (isCreated) { response.StatusCode = HttpStatusCode.OK; responseData = ResponseFormat.Success; responseData.message = SuccessMessages.CONTACT_CREATED; } } 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); }
public HttpResponseMessage Update([FromUri] int id, [FromBody] ContactCreateApiModel apiModel) { var response = new HttpResponseMessage(); ResponseFormat responseData = new ResponseFormat(); //AuthorizationService _authorizationService = new AuthorizationService().SetPerm((int)EnumPermissions.LEAD_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 = Convert.ToInt32(payload["id"]); var owner = _contactService.FindOwnerId(id); var collaborator = _contactService.FindCollaboratorId(id); if ((userId == owner) || (userId == collaborator) || (new AuthorizationService().SetPerm((int)EnumPermissions.CONTACT_DELETE).Authorize(userId))) { var isUpdated = _contactService.Update(id, apiModel, Convert.ToInt32(userId)); if (isUpdated) { response.StatusCode = HttpStatusCode.OK; responseData = ResponseFormat.Success; responseData.message = SuccessMessages.CONTACT_MODIFIED; } else { response.StatusCode = HttpStatusCode.InternalServerError; responseData = ResponseFormat.Fail; responseData.message = ErrorMessages.SOMETHING_WRONG; } } 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); }
public bool Update(int contactId, ContactCreateApiModel apiModel, int modifiedUser) { var dbContact = db.CONTACTs.Find(contactId); if (dbContact != null) { if (apiModel.owner != 0) { dbContact.ContactOwner = apiModel.owner; } if (apiModel.collaborator != 0) { dbContact.ContactCollaborator = apiModel.collaborator; } dbContact.Name = apiModel.name; dbContact.Email = apiModel.email; dbContact.Phone = apiModel.phone; dbContact.Mobile = apiModel.mobile; dbContact.DepartmentName = apiModel.departmentName; dbContact.Birthday = apiModel.birthday; if (apiModel.priority != 0) { dbContact.PRIORITY_ID = apiModel.priority; } if (apiModel.account != 0) { dbContact.ACCOUNT_ID = apiModel.account; } dbContact.NoEmail = apiModel.noEmail; dbContact.NoCall = apiModel.noCall; dbContact.Skype = apiModel.skype; dbContact.AssistantName = apiModel.assistantName; dbContact.AssistantPhone = apiModel.assistantPhone; dbContact.Country = apiModel.country; dbContact.City = apiModel.city; dbContact.ModifiedAt = DateTime.Now; dbContact.ModifiedBy = modifiedUser; db.SaveChanges(); var modifyUser = db.USERs.Find(modifiedUser); var collaborator = db.USERs.Find(dbContact.ContactCollaborator); var createdUser = db.USERs.Find(dbContact.CreatedBy); var notifyModel = new NotificationApiModel(); notifyModel.title = "Contact updated"; notifyModel.content = $"Contact {dbContact.Name} has been updated by {modifyUser.Username}."; notifyModel.module = "contacts"; notifyModel.moduleObjectId = dbContact.ID; notifyModel.createdAt = DateTime.Now; notifyModel.module = "contacts"; notifyModel.moduleObjectId = dbContact.ID; NotificationManager.SendNotification(notifyModel, new List <USER> { dbContact.Owner, collaborator, createdUser }); return(true); } else { return(false); } }
public bool Create(ContactCreateApiModel apiModel, int createdUser) { var newContact = new CONTACT(); newContact.ContactOwner = apiModel.owner != 0 ? apiModel.owner : createdUser; if (apiModel.collaborator != 0) { newContact.ContactCollaborator = apiModel.collaborator; } newContact.Name = apiModel.name; newContact.Email = apiModel.email; newContact.Phone = apiModel.phone; newContact.Mobile = apiModel.mobile; newContact.DepartmentName = apiModel.departmentName; newContact.Birthday = apiModel.birthday; if (apiModel.account != 0) { newContact.ACCOUNT_ID = apiModel.account; } if (apiModel.priority != 0) { newContact.PRIORITY_ID = apiModel.priority; } newContact.NoEmail = apiModel.noEmail; newContact.NoCall = apiModel.noCall; newContact.Skype = apiModel.skype; newContact.AssistantName = apiModel.assistantName; newContact.AssistantPhone = apiModel.assistantPhone; newContact.Country = apiModel.country; newContact.City = apiModel.city; newContact.AddressDetail = apiModel.addressDetail; newContact.CreatedAt = DateTime.Now; newContact.CreatedBy = createdUser; newContact.ModifiedAt = DateTime.Now; try { db.CONTACTs.Add(newContact); db.SaveChanges(); var owner = db.USERs.Find(newContact.ContactOwner); var collaborator = db.USERs.Find(newContact.ContactCollaborator); var creator = db.USERs.Find(createdUser); var notifyModel = new NotificationApiModel(); notifyModel.title = "Contact assigned"; notifyModel.content = $"Contact {newContact.Name} has been created and assigned to you by {creator?.Username}."; notifyModel.createdAt = DateTime.Now; notifyModel.module = "contacts"; notifyModel.moduleObjectId = newContact.ID; NotificationManager.SendNotification(notifyModel, new List <USER> { owner, collaborator }); return(true); } catch { return(false); } }