public CaseCategoryDto CreateCaseCategory(CaseCategoryDto model) { if (model == null) { throw new ArgumentNullException(nameof(model), "Model is null!"); } var sameName = _dbContext.CaseCategory.FirstOrDefault(x => x.CaseCategoryName == model.CaseCategoryName && x.IsDeleted == false); if (sameName != null) { throw new Exception("Case category with this name already exists."); } try { var caseCategory = Mappers.AdminRepository.MapToDbEntity(model); caseCategory.DateModified = caseCategory.DateCreated = DateTime.Now; caseCategory.IsDeleted = false; _dbContext.Add(caseCategory); if (_dbContext.SaveChanges() != 0) { return(Mappers.AdminRepository.MapToDto(caseCategory)); } } catch (Exception ex) { Console.WriteLine(ex.InnerException); throw new Exception("Cannot create new case category!"); } return(null); }
public AddressDto CreateAddress(AddressDto addressDto) { if (addressDto == null) { throw new ArgumentNullException(nameof(addressDto), "AddresDto argument is not provided!"); } try { var address = Mappers.AddressRepository.MapToDbEntity(addressDto); address.DateCreated = DateTime.Now; address.DateModified = DateTime.Now; _dbContext.Add(address); if (_dbContext.SaveChanges() != 0) { return(Mappers.AddressRepository.MapToDto(address)); } } catch (Exception ex) { Console.WriteLine(ex.InnerException); throw; } return(null); }
public CaseCategoryDto CreateCaseCategory(CaseCategoryDto caseCategoryDto) { try { var caseCategory = Mappers.AdminRepository.MapToDbEntity(caseCategoryDto); caseCategory.DateModified = caseCategory.DateCreated = DateTime.Now; caseCategory.IsDeleted = false; _dbContext.Add(caseCategory); if (_dbContext.SaveChanges() != 0) { return(Mappers.AdminRepository.MapToDto(caseCategory)); } } catch (Exception ex) { //log ex throw new Exception(ex.Message); } return(null); }
public ContactDto CreateContact(ContactDto contactDto, int caseId) { try { this.addressRepository = new AddressRepository(_dbContext); DC.AddressRepository.AddressDto address = null; if (contactDto.Address != null) { address = addressRepository.CreateAddress(contactDto.Address); contactDto.AddressId = address.AddressId; } var contact = Mappers.ContactRepository.MapToDbEntity(contactDto); contact.ModifiedDate = contact.CreatedDate = DateTime.Now; contact.IsDeleted = false; _dbContext.Add(contact); if (_dbContext.SaveChanges() != 0) { ContactDto c = Mappers.ContactRepository.MapToDto(contact); if (caseId != 0) { CaseContact caseInfo = new CaseContact(); caseInfo.CaseId = caseId; caseInfo.Contact = c.Contact1; this._dbContext.CaseContact.Add(caseInfo); } if (_dbContext.SaveChanges() != 0) { return(c); } } } catch (Exception ex) { //log ex throw new Exception(ex.Message); } return(null); }
int IDocumentRepository.SaveDocument(CreateDocumentDto document) { try { document.DocumentId = 0; var documentEntity = DocumentRepository.MapToDbEntity(document, _dbContext); _dbContext.Add(documentEntity); var result = _dbContext.SaveChanges(); AddToHistory(documentEntity); return(result); } catch (Exception ex) { throw new Exception(ex.Message); } }
DocumentDetails IDocumentRepository.SaveDocument(CreateDocumentDto document) { try { document.DocumentId = 0; var documentEntity = DocumentRepository.MapToDbEntity(document, _dbContext); _dbContext.Add(documentEntity); _dbContext.SaveChanges(); AddToHistory(documentEntity); return(DocumentRepository.MapToDbEntity(documentEntity)); } catch (Exception ex) { Logger.Logger.LogError(ex.Message); throw new NSIException(ex.Message, DC.Exceptions.Enums.Level.Error, DC.Exceptions.Enums.ErrorType.InvalidParameter); } }
public TaskDto CreateTask(TaskDto taskDto) { if (taskDto == null) { throw new NSIException("Parameter taskDto is null!", Level.Error, ErrorType.InvalidParameter); } var task = Mappers.TaskRepository.MapToDbEntity(taskDto); _dbContext.Add(task); if (_dbContext.SaveChanges() != 0) { return(Mappers.TaskRepository.MapToDto(task)); } throw new NSIException("No data in database!", Level.Info, ErrorType.MissingData); }
public ClientDTO CreateClient(ClientDTO clientDTO) { try { var client = Mappers.ClientRepository.MapToDbEntity(clientDTO); client.DateCreated = DateTime.Now; client.DateModified = null; _dbContext.Add(client); if (_dbContext.SaveChanges() != 0) { return(Mappers.ClientRepository.MapToDto(client)); } } catch (Exception ex) { //log ex throw ex.InnerException; //throw new Exception("Database error!"); } return(null); }
public ClientDto CreateClient(ClientDto clientDTO) { try { var client = Mappers.ClientRepository.MapToDbEntity(clientDTO); client.ClientId = null; client.DateCreated = DateTime.Now; client.DateModified = null; client.IsDeleted = false; _dbContext.Add(client); if (_dbContext.SaveChanges() != 0) { return(Mappers.ClientRepository.MapToDto(client)); } } catch (Exception ex) { throw new NSIException(ex.InnerException.Message, Level.Error, ErrorType.InvalidParameter, HttpStatusCode.BadRequest); } return(null); }
public CustomerDto CreateCustomer(CustomerDto customerDto) { try { var customer = Mappers.CustomerRepository.MapToDbEntity(customerDto); customer.DateCreated = DateTime.Now; customer.DateModified = null; _dbContext.Add(customer); if (_dbContext.SaveChanges() != 0) { return(Mappers.CustomerRepository.MapToDto(customer)); } } catch (Exception ex) { //log ex throw ex.InnerException; //throw new Exception("Database error!"); } return(null); }
public CustomerDto CreateCustomer(CustomerDto customerDto) { try { var customer = Mappers.CustomerRepository.MapToDbEntity(customerDto); customer.CustomerId = null; customer.DateCreated = DateTime.Now; customer.DateModified = DateTime.Now; _dbContext.Add(customer); if (_dbContext.SaveChanges() != 0) { return(Mappers.CustomerRepository.MapToDto(customer)); } } catch (Exception ex) { if (ex.InnerException.GetType().ToString() == "Npgsql.PostgresException") { PostgresException postgresException = (PostgresException)ex.InnerException; if (postgresException.SqlState == "23503") { throw new NSIException("Foreign key error: " + postgresException.Detail, Level.Error, ErrorType.InvalidParameter); } if (postgresException.SqlState == "23505") { throw new NSIException("Primary key error: " + postgresException.Detail, Level.Error, ErrorType.InvalidParameter); } throw new NSIException("Db error: " + postgresException.Detail, Level.Error, ErrorType.DBError); } else { throw ex.InnerException; } } return(null); }
public AddressTypeDto CreateAddressType(AddressTypeDto addressTypeDto) { if (addressTypeDto == null) { throw new ArgumentNullException(nameof(addressTypeDto), "AddressTypeDto is not provided!"); } try { var addressType = Mappers.AddressTypeRepository.MapToDbEntity(addressTypeDto); _dbContext.Add(addressType); if (_dbContext.SaveChanges() != 0) { return(Mappers.AddressTypeRepository.MapToDto(addressType)); } } catch (Exception ex) { //log ex throw; } return(null); }