async public Task<IActionResult> CustomerUpdateById([FromBody] Shared.Customer model, string folderId, string customerId) { string userId = User.GetUserId(); if (string.IsNullOrEmpty(model.Name)) { return BadRequest("Customer name is required"); } var customer = await _db.Customers.Where(c => c.FolderId == folderId && c.Id == customerId).FirstOrDefaultAsync(); if (customer == null) return BadRequest("Customer not found"); customer.Name = model.Name; customer.Email = model.Email; customer.Phone = model.Phone; customer.BirthDate = model.BirthDate; customer.Address = model.Address; customer.City = model.City; customer.State = model.State; customer.Postal = model.Postal; customer.Notes = model.Notes; customer.ImageBase64 = model.ImageBase64; customer.UpdateOn = DateTime.UtcNow; await _db.SaveChangesAsync(); Shared.Customer response = customer.ToSharedCustomer(); response.Files = await _db.CustomerFiles.Where(c => c.Customer.FolderId == folderId && c.CustomerId == customerId).Select(f => new ghostlight.Shared.CustomerFile() { Id = f.Id, Name = f.PrettyFileName, Uploaded = f.Uploaded }).ToListAsync(); return Ok(response); }
async public Task<IActionResult> CustomerCreate([FromBody] Shared.Customer model, string folderId) { string userId = User.GetUserId(); if (string.IsNullOrEmpty(model.Name)) { return BadRequest("Customer name is required"); } ghostlight.Server.Entities.Customer customer = new ghostlight.Server.Entities.Customer() { Name = model.Name, Email = model.Email, Phone = model.Phone, BirthDate = model.BirthDate, Address = model.Address, City = model.City, State = model.State, Postal = model.Postal, Notes = model.Notes, ImageBase64 = model.ImageBase64, FolderId = folderId }; _db.Customers.Add(customer); await _db.SaveChangesAsync(); Shared.Customer response = customer.ToSharedCustomer(); //Not necessary at this time. Not files are automatically uploaded as part of customer creation. //response.Files = await _db.CustomerFiles.Where(c => c.Customer.FolderId == folderId && c.CustomerId == customerId).Select(f => new ghostlight.Shared.CustomerFile() { Id = f.Id, Name = f.PrettyFileName, Uploaded = f.Uploaded }).ToListAsync(); return Ok(response); }
async public Task<IActionResult> CustomerGetById(string folderId, string customerId) { string userId = User.GetUserId(); var customer = await _db.Customers.Where(c => c.FolderId == folderId && c.Id == customerId).FirstOrDefaultAsync(); if (customer == null) return BadRequest("Customer not found"); Shared.Customer response = customer.ToSharedCustomer(); response.Files = await _db.CustomerFiles.Where(c => c.Customer.FolderId == folderId && c.CustomerId == customerId).Select(f => new ghostlight.Shared.CustomerFile() { Id = f.Id, Name = f.PrettyFileName, Uploaded = f.Uploaded }).ToListAsync(); return Ok(response); }
static public Shared.Customer ToSharedCustomer(this Entities.Customer model) { var customer = new Shared.Customer() { Id = model.Id, Name = model.Name, Email = model.Email, Phone = model.Phone, Address = model.Address, City = model.City, State = model.State, Postal = model.Postal, Notes = model.Notes, BirthDate = model.BirthDate.HasValue ? model.BirthDate.Value : null, Gender = model.Gender, Active = model.Active, ImageBase64 = model.ImageBase64 }; return(customer); }