protected async Task CreateSingleRecord(Guid workerId, DateTime date, ServiceCalendarRecord record) { await serviceCalendarRepository.WriteAsync(shop.Id, new WorkerCalendarDay <ServiceCalendarRecord> { Date = date, WorkerId = workerId, Records = new[] { record } }); }
private ServiceCalendarRecordBuilder(Guid recordId, TimePeriod period) { record = new ServiceCalendarRecord { Id = recordId, Period = period, CustomerStatus = CustomerStatus.Active, RecordStatus = RecordStatus.Active, ProductIds = new Guid[0], CustomerId = null, Comment = "", }; }
public async Task WriteThenRead() { var shopId = Guid.NewGuid(); var workerId = Guid.NewGuid(); var record = new ServiceCalendarRecord { Period = TimePeriod.CreateByHours(12, 13), Comment = "test" }; record.Id = await serviceCalendarApiClient.CreateRecord(shopId, DateTime.Now, workerId, record); var day = await serviceCalendarApiClient.GetShopDay(shopId, DateTime.Now); day.WorkerCalendarDays.Single().Records.Single().Should().BeEquivalentTo(record); }
private async Task <ValidationResult> UpdateSingleRecordAsync(Guid shopId, DateTime date, Guid workerId, ServiceCalendarRecord record) { using (await locker.LockAsync(GetLockId(shopId, workerId, date))) { var workerCalendarDay = await calendarRepository.ReadWorkerCalendarDayAsync(shopId, date, workerId); var currentRecord = GetRecord(workerCalendarDay, record.Id); if (currentRecord == null) { return(ValidationResult.Fail("recordNotFound", $"no record with id {record.Id}")); } if (!ValidateSlot(workerCalendarDay, record.Id, record.Period)) { return(ValidationResult.Fail("periodSlot", "period is not empty")); } record.CustomerStatus = currentRecord.CustomerStatus; record.RecordStatus = currentRecord.RecordStatus; workerCalendarDay.Records = workerCalendarDay.Records.Where(x => x.Id != record.Id).Append(record).ToArray(); await calendarRepository.WriteAsync(shopId, workerCalendarDay); return(ValidationResult.Success()); } }
public async Task <ValidationResult> UpdateAsync(Guid shopId, DateTime date, Guid workerId, ServiceCalendarRecord record, DateTime?updateDate, Guid?updateWorkerId) { var validationResult = serviceCalendarRecordValidator.Validate(record); if (!validationResult.IsSuccess) { return(validationResult); } if ((updateDate == null || updateDate.Value == date) && (updateWorkerId == null || updateWorkerId.Value == workerId)) { return(await UpdateSingleRecordAsync(shopId, date, workerId, record)); } return(await UpdateTwoRecordsAsync(shopId, date, workerId, record, updateDate ?? date, updateWorkerId ?? workerId)); }
public async Task <(Guid recordId, ValidationResult validationResult)> CreateAsync(Guid shopId, DateTime date, Guid workerId, ServiceCalendarRecord record) { var validationResult = serviceCalendarRecordValidator.Validate(record); if (!validationResult.IsSuccess) { return(Guid.Empty, validationResult); } record.Id = Guid.NewGuid(); record.RecordStatus = RecordStatus.Active; record.CustomerStatus = CustomerStatus.Active; using (await locker.LockAsync(GetLockId(shopId, workerId, date))) { var workerCalendarDay = await calendarRepository.ReadWorkerCalendarDayAsync(shopId, date, workerId); if (!ValidateSlot(workerCalendarDay, record.Id, record.Period)) { return(record.Id, ValidationResult.Fail("periodSlot", "period is not empty")); } workerCalendarDay.Records = workerCalendarDay.Records.Append(record).ToArray(); await calendarRepository.WriteAsync(shopId, workerCalendarDay); return(record.Id, ValidationResult.Success()); } }
private async Task <ValidationResult> UpdateTwoRecordsAsync(Guid shopId, DateTime date, Guid workerId, ServiceCalendarRecord record, DateTime updateDate, Guid updateWorkerId) { var locks = new[] { GetLockId(shopId, workerId, date), GetLockId(shopId, updateWorkerId, updateDate) }.OrderBy(x => x).ToArray(); using (await locker.LockAsync(locks[0])) using (await locker.LockAsync(locks[1])) { var workerCalendarDay = await calendarRepository.ReadWorkerCalendarDayAsync(shopId, date, workerId); var updateWorkerCalendarDay = await calendarRepository.ReadWorkerCalendarDayAsync(shopId, updateDate, updateWorkerId); var currentRecord = GetRecord(workerCalendarDay, record.Id); if (currentRecord == null) { return(ValidationResult.Fail("recordNotFound", $"no record with id {record.Id}")); } if (!ValidateSlot(updateWorkerCalendarDay, record.Id, record.Period)) { return(ValidationResult.Fail("periodSlot", "period is not empty")); } record.CustomerStatus = currentRecord.CustomerStatus; record.RecordStatus = currentRecord.RecordStatus; workerCalendarDay.Records = workerCalendarDay.Records.Where(x => x.Id != record.Id).ToArray(); updateWorkerCalendarDay.Records = updateWorkerCalendarDay.Records.Append(record).ToArray(); if (DateHelper.GetFirstDayOfMonth(date) == DateHelper.GetFirstDayOfMonth(updateDate)) { await calendarRepository.WriteAsync(shopId, new[] { updateWorkerCalendarDay, workerCalendarDay }); } else { await calendarRepository.WriteAsync(shopId, updateWorkerCalendarDay); await calendarRepository.WriteAsync(shopId, workerCalendarDay); } return(ValidationResult.Success()); } }
public async Task <Guid> CreateRecord(Guid shopId, DateTime date, Guid workerId, ServiceCalendarRecord record) { var request = Request.Post(Url(shopId, date, c => c.AppendToPath("workers").AppendToPath(workerId).AppendToPath("records"))) .WithContentTypeApplicationJsonHeader() .WithContent(record.SerializeDto()); var result = await clusterClient.SendAsync( request ); return(result.DeserializeDto <Guid>()); }
public async Task <ActionResult> CreateRecord([FromRoute] Guid shopId, [FromRoute] DateTime date, [FromRoute] Guid workerId, [FromBody] ServiceCalendarRecord record) { var periodValidation = periodValidator.Validate(record); if (!periodValidation.IsSuccess) { return(BadRequest($"{periodValidation.ErrorType} - {periodValidation.ErrorDescription}")); } var(recordId, validationResult) = await calendarService.CreateAsync(shopId, date, workerId, record); if (!validationResult.IsSuccess) { return(BadRequest($"{validationResult.ErrorType} - {validationResult.ErrorDescription}")); } return(Ok(recordId)); }