public async Task <ActionResult <Clocking> > PostClockings(Clocking clockings)
        {
            _context.Clockings.Add(clockings);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetClockings", new { id = clockings.ClockingId }, clockings));
        }
        public async Task <IActionResult> PutClockings(int id, Clocking clockings)
        {
            if (id != clockings.ClockingId)
            {
                return(BadRequest());
            }

            _context.Entry(clockings).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClockingsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
示例#3
0
 public ActionResult End()
 {
     Clocking.Action(Clocking.ClockStatus.End);
     return Json(new { success = "true" }, JsonRequestBehavior.AllowGet);
 }
示例#4
0
        public async Task <ActionResult <ButtonStateEnum> > ChangeButtonState([FromBody] MyDate crtDate)
        {
            var currentDate = crtDate.CurrentDate;
            var userId      = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var buttonState = await GetButtonState(currentDate);

            Console.Write(buttonState);
            // butonul e apasat => pontajul e inceput
            if (buttonState.Value == ButtonStateEnum.OngoingClocking)
            {
                // inchidem pontajul
                var clocking = await _context.Clockings
                               .Include(cl => cl.Date)
                               .FirstOrDefaultAsync(cl =>
                                                    cl.EndTime == DateTime.MinValue &&
                                                    cl.Date.UserId == userId);

                clocking.EndTime = currentDate;
                _context.Entry(clocking).State = EntityState.Modified;

                await _context.SaveChangesAsync();


                var time = clocking.EndTime - clocking.StartTime;


                var date = await _context.Dates
                           .FirstOrDefaultAsync(d =>
                                                d.CurrentDate.Day == currentDate.Day &&
                                                d.CurrentDate.Month == currentDate.Month &&
                                                d.CurrentDate.Year == currentDate.Year &&
                                                d.UserId == userId);

                if (date.Seconds + time.Seconds >= 60)
                {
                    date.Minutes++;
                    date.Seconds = date.Seconds + time.Seconds - 60;
                }

                if (date.Minutes + time.Minutes >= 60)
                {
                    date.Hours++;
                    date.Minutes = date.Minutes + time.Minutes - 60;
                }

                date.Seconds += time.Seconds;
                date.Minutes += time.Minutes;
                date.Hours   += time.Hours;

                _context.Entry(date).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(Ok(ButtonStateEnum.NewClocking));
            }

            else
            {
                if (buttonState.Value == ButtonStateEnum.FirstClocking)
                {
                    Date date = new Date
                    {
                        CurrentDate = currentDate,
                        UserId      = userId,
                        Hours       = 0,
                        Minutes     = 0,
                        Seconds     = 0
                    };

                    _context.Dates.Add(date);
                    await _context.SaveChangesAsync();

                    Clocking clocking = new Clocking
                    {
                        DateId    = date.DateId,
                        StartTime = currentDate,
                        EndTime   = DateTime.MinValue
                    };

                    _context.Clockings.Add(clocking);
                    await _context.SaveChangesAsync();

                    return(Ok(ButtonStateEnum.OngoingClocking));
                }

                else
                {
                    if (buttonState.Value == ButtonStateEnum.NewClocking)
                    {
                        var date = await _context.Dates
                                   .FirstOrDefaultAsync(d =>
                                                        d.CurrentDate.Day == currentDate.Day &&
                                                        d.CurrentDate.Month == currentDate.Month &&
                                                        d.CurrentDate.Year == currentDate.Year &&
                                                        d.UserId == userId);


                        Clocking clocking = new Clocking
                        {
                            DateId    = date.DateId,
                            StartTime = currentDate,
                            EndTime   = DateTime.MinValue
                        };

                        _context.Clockings.Add(clocking);
                        await _context.SaveChangesAsync();

                        return(Ok(ButtonStateEnum.OngoingClocking));
                    }
                }
            }

            return(Ok(true));
        }