Example #1
0
        private async Task createParentTableIfDoestExist(CzasStartStopViewModel model)
        {
            bool wasStepEarlierStarted;

            int wasStepEarlierStartedCountItems = await _dbContext.PracownikCzasStep.CountAsync(x =>
                                                                                                x.PracownikId == model.PracownikId &&
                                                                                                x.OfferLinesId == model.OfferLineId &&
                                                                                                x.ProcessId == model.ProcessId &&
                                                                                                x.Step == model.StepNum &&
                                                                                                x.CzasStop != null);

            wasStepEarlierStarted = wasStepEarlierStartedCountItems > 0;

            if (!wasStepEarlierStarted)
            {
                var itemToUpdateStartedTime = await _dbContext.StepOfferWykonanie.FirstOrDefaultAsync(x =>
                                                                                                      x.OfferLineId == model.OfferLineId &&
                                                                                                      x.ProcessId == model.ProcessId &&
                                                                                                      x.Step == model.StepNum);

                if (itemToUpdateStartedTime == null)
                {
                    AddStepOfferWykonanie(new SaveCompleteStepViewModel()
                    {
                        OfferLineId = model.OfferLineId,
                        ProcessId   = model.ProcessId,
                        StepNum     = model.StepNum
                    }, this.GetUserId());
                }
                else
                {
                    itemToUpdateStartedTime.StartedOn = DateTime.Now;
                }
            }
        }
Example #2
0
        private async Task CheckIsStepClosed(CzasStartStopViewModel model)
        {
            var isStepFinished = await _dbContext.StepOfferWykonanie.AnyAsync(p =>
                                                                              p.ProcessId == model.ProcessId &&
                                                                              p.OfferLineId == model.OfferLineId &&
                                                                              p.Step == model.StepNum && p.Zakonczonie == true);

            if (isStepFinished)
            {
                throw new StepAlreadyClosedException("Step został już zakończony i pomiary czasu zostaly zastopowane");
            }
        }
Example #3
0
        public async Task <IActionResult> TimeStart([FromBody] CzasStartStopViewModel model)
        {
            try
            {
                var userId = this.GetUserId();
                model.PracownikId = userId;

                var OtherStepStarted = await _dbContext.PracownikCzasStep.FirstOrDefaultAsync(p =>
                                                                                              p.PracownikId == model.PracownikId &&
                                                                                              p.CzasStart != null &&
                                                                                              p.CzasStop == null);

                // nie trzeba stprawdzac czy ten step jest otwarty, bo to sprawdza ogolnie wszystkie stepy
                if (OtherStepStarted != null)
                {
                    throw new AnotherStepOpenedException($"{OtherStepStarted.ProcessId}-{OtherStepStarted.OfferLinesId}");
                }

                await createParentTableIfDoestExist(model);

                _dbContext.PracownikCzasStep.Add(new PracownikCzasStep()
                {
                    Id           = 0,
                    CzasStart    = DateTime.Now,
                    OfferLinesId = model.OfferLineId,
                    PracownikId  = userId,
                    ProcessId    = model.ProcessId,
                    Step         = model.StepNum
                });
                await _dbContext.SaveChangesAsync();

                return(Ok());
            }
            catch (AnotherStepOpenedException ex)
            {
                return(BadRequest("Jest już otwarty inny step w linii - " + ex.StepOpenedId));
            }
            catch (StepAlreadyClosedException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Example #4
0
        private async Task <int> StopTime(CzasStartStopViewModel model)
        {
            int timeToAdd          = 0;
            PracownikCzasStep find = null;

            find = await _dbContext.PracownikCzasStep.FirstOrDefaultAsync(x =>
                                                                          x.PracownikId == model.PracownikId &&
                                                                          x.OfferLinesId == model.OfferLineId &&
                                                                          x.ProcessId == model.ProcessId &&
                                                                          x.Step == model.StepNum &&
                                                                          x.CzasStop == null);

            if (find != null)
            {
                timeToAdd        = (int)DateTime.Now.Subtract(find.CzasStart.Value).TotalMinutes;
                find.LiczbaMinut = timeToAdd;
                find.CzasStop    = DateTime.Now;
            }
            return(timeToAdd);
        }
Example #5
0
        public async Task <IActionResult> TimeStop([FromBody] CzasStartStopViewModel model)
        {
            try
            {
                var userId = this.GetUserId();
                model.PracownikId = userId;
                await CheckIsStepClosed(model);

                var timeToAdd = await StopTime(model);

                await _dbContext.SaveChangesAsync();

                return(Ok(timeToAdd));
            }
            catch (StepAlreadyClosedException ex)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, "Step został już zakończony." + ex));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }