public async Task <object> Register(Employee newEmployee)
        {
            ObjectResponse response;

            try
            {
                Employee employee = (await Context.Employees.Where(
                                         e => e.IdNumber == newEmployee.IdNumber).ToListAsync()).LastOrDefault();
                if (employee == null)
                {
                    AreaManager manager = Context.AreaManagers.Where(
                        am => am.IdNumber == newEmployee.IdNumber).ToList().LastOrDefault();
                    if (manager == null)
                    {
                        await Context.Employees.AddAsync(newEmployee);

                        await Context.SaveChangesAsync();

                        response = new ObjectResponse
                        {
                            IsPassed = true,
                            Value    = newEmployee
                        };

                        return(response);
                    }
                }

                response = new ObjectResponse
                {
                    IsPassed    = false,
                    IsException = false,
                    Value       = "The id number it's allredy exist"
                };

                return(response);
            }
            catch
            {
                response = new ObjectResponse
                {
                    IsPassed    = false,
                    IsException = true,
                    Value       = "Somthing rong with the database please try again"
                };

                return(response);
            }
        }
Exemplo n.º 2
0
        public async Task <object> Create(WorkShift workShift)
        {
            ObjectResponse response;

            try
            {
                await Context.WorkShifts.AddAsync(workShift);

                await Context.SaveChangesAsync();

                response = new ObjectResponse
                {
                    IsPassed = true,
                    Value    = workShift
                };

                ActivityRecord activityRecord;
                try
                {
                    activityRecord = new ActivityRecord
                    {
                        Action    = Enums.ActionType.Create,
                        ManagerId = (await Context.AreaManagers.Where(
                                         ma => ma.AreaId == workShift.WorkDay.Position.AreaId)
                                     .ToListAsync()).LastOrDefault().Id,
                        Time        = DateTime.Now,
                        WorkShiftId = workShift.Id
                    };

                    await Context.ActivityRecords.AddAsync(activityRecord);

                    await Context.SaveChangesAsync();
                }
                catch
                {
                    activityRecord = new ActivityRecord
                    {
                        Action    = Enums.ActionType.Create,
                        ManagerId = (await Context.AreaManagers.Where(
                                         ma => ma.AreaId == workShift.WorkDay.Position.AreaId)
                                     .ToListAsync()).LastOrDefault().Id,
                        Time        = DateTime.Now,
                        WorkShiftId = workShift.Id
                    };

                    await Context.ActivityRecords.AddAsync(activityRecord);

                    await Context.SaveChangesAsync();
                }

                return(response);
            }
            catch
            {
                response = new ObjectResponse
                {
                    IsPassed    = false,
                    IsException = true,
                    Value       = "Somthing rong with the database please try again"
                };

                return(response);
            }
        }