public async Task <ApiResponse <bool> > EditBarber(EditBarberDTO model) { ApiResponse <bool> result = new ApiResponse <bool>(); try { Barber BarberToEdit = await unitOfWork.BarbersManager.GetByIdAsync(model.Id); if (BarberToEdit != null) { BarberProfilePhoto barberProfilePhotoToEdit = await unitOfWork.BarberProfilePhotosManager.GetProfilePhotoByBarberId(BarberToEdit.Id); if (barberProfilePhotoToEdit != null) { BarberToEdit.NameAR = model.NameAR; BarberToEdit.NameEN = model.NameEN; BarberToEdit.BarberFingerPrintId = model.BarberFingerprintId; var barberPrintResult = await unitOfWork.BarbersManager.GetAsync(a => a.BarberFingerPrintId == model.BarberFingerprintId); var barber = barberPrintResult.FirstOrDefault(); if (barber != null && model.Id != BarberToEdit.Id) { result.Succeeded = false; result.Errors.Add("Finger Print Id is used ! "); return(result); } else { barberProfilePhotoToEdit.AdminPath = model.BarberProfilePhotoPathAdmin; barberProfilePhotoToEdit.UserPath = model.BarberProfilePhotoPathUser; var barberResult = await unitOfWork.BarbersManager.UpdateAsync(BarberToEdit); var barberProfilePhotoResult = await unitOfWork.BarberProfilePhotosManager.UpdateAsync(barberProfilePhotoToEdit); if (barberResult == true && barberProfilePhotoResult == true) { await unitOfWork.SaveChangesAsync(); result.Succeeded = true; result.Data = true; return(result); } else { result.Succeeded = false; result.Errors.Add("Failed to update barber information ! "); return(result); } } } else { result.Succeeded = false; result.Errors.Add("Unable to find barber profile photo with specified barber id ! "); return(result); } } else { result.Succeeded = false; result.Errors.Add("Unable to find barber with specified id ! "); return(result); } } catch (Exception ex) { result.Succeeded = false; result.Errors.Add(ex.Message); return(result); } }
public async Task <ApiResponse <BarberDTO> > CreateBarber(CreateBarberDTO model) { ApiResponse <BarberDTO> result = new ApiResponse <BarberDTO>(); try { var barberPrintResult = await unitOfWork.BarbersManager.GetAsync(a => a.BarberFingerPrintId == model.BarberFingerprintId); var barber = barberPrintResult.FirstOrDefault(); if (barber == null) { var shops = await unitOfWork.ShopsManager.GetAsync(); Shop shop = shops.FirstOrDefault(); Barber barberToAdd = new Barber(); barberToAdd.NameAR = model.NameAR; barberToAdd.NameEN = model.NameEN; barberToAdd.ShopId = shop.Id; barberToAdd.NumberOfCustomersHandled = 0; barberToAdd.BarberFingerPrintId = model.BarberFingerprintId; barberToAdd.Status = "Unavailable"; var barberResult = await unitOfWork.BarbersManager.CreateAsync(barberToAdd); await unitOfWork.SaveChangesAsync(); if (barberResult != null) { BarberProfilePhoto barberProfilePhoto = new BarberProfilePhoto(); barberProfilePhoto.BarberId = barberResult.Id; barberProfilePhoto.AdminPath = model.BarberProfilePhotoPathAdmin; barberProfilePhoto.UserPath = model.BarberProfilePhotoPathUser; var barberProfilePhotoResult = await unitOfWork.BarberProfilePhotosManager.CreateAsync(barberProfilePhoto); await unitOfWork.SaveChangesAsync(); if (barberProfilePhotoResult != null) { BarberQueue barberQueue = new BarberQueue(); barberQueue.BarberId = barberResult.Id; barberQueue.QueueStatus = "idle"; barberQueue.QueueWaitingTime = 0; var barberQueueResult = await unitOfWork.BarbersQueuesManager.CreateAsync(barberQueue); await unitOfWork.SaveChangesAsync(); if (barberQueueResult != null) { var barbers = await unitOfWork.BarbersManager.GetAsync(b => b.Id == barberResult.Id, includeProperties : "BarberQueue,BarberProfilePhoto"); Barber barberToReturn = barbers.FirstOrDefault(); if (barberToReturn != null) { result.Succeeded = true; result.Data = mapper.Map <BarberDTO>(barberToReturn); result.Errors.Add("Failed to create barber !"); return(result); } else { result.Succeeded = false; result.Errors.Add("Error creating barber !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Error creating barber queue !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Failed to create barber profile photo !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("A barber already exists with the specified finger print id !"); return(result); } } else { result.Succeeded = false; result.Errors.Add("Failed to create barber !"); return(result); } } catch (Exception ex) { result.Succeeded = false; result.Errors.Add(ex.Message); return(result); } }