public async Task <Tuple <Proposal, string> > CreateProposal(ProposalDto proposalDto)
        {
            var proposal = _mapper.Map <Proposal>(proposalDto);

            var stageTuple = await GetStage(proposalDto.StageId, proposalDto.Stage);

            if (stageTuple?.Item2 != null)
            {
                return(new Tuple <Proposal, string>(null, stageTuple.Item2));
            }
            else if (stageTuple?.Item1 != null)
            {
                proposal.Stage = stageTuple.Item1;
            }

            var deadlineTuple = await GetDeadline(proposalDto.DeadlineId, proposalDto.Deadline);

            if (deadlineTuple?.Item2 != null)
            {
                return(new Tuple <Proposal, string>(null, deadlineTuple.Item2));
            }
            else if (deadlineTuple?.Item1 != null)
            {
                proposal.Deadline = deadlineTuple.Item1;
            }

            _dbContext.Proposals.Add(proposal);
            await _dbContext.SaveChangesAsync();

            return(new Tuple <Proposal, string>(proposal, null));
        }
Esempio n. 2
0
        public async Task <IActionResult> PutUsers(int id, User users)
        {
            if (id != users.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Esempio n. 3
0
        public async Task <IActionResult> PutSchools(int id, SchoolDto schoolDto)
        {
            if (schoolDto.Id == 0)
            {
                schoolDto.Id = id;
            }
            if (id != schoolDto.Id)
            {
                return(BadRequest());
            }

            var school = await _context.Schools.FirstOrDefaultAsync(_ => _.Id == schoolDto.Id);

            if (school == null)
            {
                return(NotFound());
            }
            if (schoolDto.Name != null)
            {
                school.Name = schoolDto.Name;
            }
            if (schoolDto.Display != null)
            {
                school.Display = schoolDto.Display.Value;
            }
            school.DateModified          = DateTime.Now;
            _context.Entry(school).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(RedirectToAction("GetSchools", new { id = schoolDto.Id }));
        }
        public async Task <ActionResult <Proposal> > DeleteProposals(int id)
        {
            var proposals = await _context.Proposals.FindAsync(id);

            if (proposals == null)
            {
                return(NotFound());
            }

            _context.Proposals.Remove(proposals);
            await _context.SaveChangesAsync();

            return(proposals);
        }
        public async Task <Tuple <AuthUserDto, string> > CheckOrCreate(LoginDto loginDto)
        {
            var isSchoolExist = await _dbContext.Schools.AnyAsync(_ => _.Id == loginDto.SchoolId);

            if (!isSchoolExist)
            {
                return(new Tuple <AuthUserDto, string>(
                           null,
                           "School doesn't exist"
                           ));
            }
            var user = await _dbContext.Users.Include(_ => _.UserType)
                       .FirstOrDefaultAsync(_ => _.Nickname == loginDto.Nickname);

            if (user != null)
            {
                if (user.Code != loginDto.Code)
                {
                    return(null);
                }
                if ((loginDto.UserTypeId != null && loginDto.UserTypeId != user.UserTypeId) ||
                    (loginDto.UserType != null && loginDto.UserType != user.UserType.Name))
                {
                    return(new Tuple <AuthUserDto, string>(
                               null,
                               "User role is incorrect"
                               ));
                }
                if (user.Name != loginDto.Name)
                {
                    user.Name         = loginDto.Name;
                    user.DateModified = DateTime.Now;
                    _dbContext.Update(user);
                    await _dbContext.SaveChangesAsync();
                }
                return(new Tuple <AuthUserDto, string>(
                           new AuthUserDto
                {
                    UserId = user.Id,
                    Nickname = user.Nickname,
                    SchoolId = user.SchoolId,
                    UserTypeId = user.UserTypeId,
                    UserType = user.UserType.Name
                },
                           null
                           ));
            }
            else
            {
                user = new User
                {
                    Name          = loginDto.Name,
                    Nickname      = loginDto.Nickname,
                    SchoolId      = loginDto.SchoolId,
                    Code          = loginDto.Code,
                    CodeGenerated = DateTime.Now,
                    DateModified  = DateTime.Now,
                    DateCreated   = DateTime.Now
                };

                var userType = await _dbContext.UserTypes.FirstOrDefaultAsync(_ =>
                                                                              _.Id == loginDto.UserTypeId || _.Name == loginDto.UserType);

                if ((loginDto.UserTypeId != null || loginDto.UserType != null) &&
                    userType == null)
                {
                    return(new Tuple <AuthUserDto, string>(
                               null,
                               "User type not found"
                               ));
                }
                if (loginDto.UserTypeId != null && loginDto.UserType != null)
                {
                    if (userType.Id != loginDto.UserTypeId || userType.Name != loginDto.UserType)
                    {
                        return(new Tuple <AuthUserDto, string>(
                                   null,
                                   "User type is inconsistent"
                                   ));
                    }
                }
                if (userType == null)
                {
                    userType = new UserType
                    {
                        Id = 1
                    };
                }
                user.UserTypeId = userType.Id;
                _dbContext.Add(user);
                await _dbContext.SaveChangesAsync();

                return(new Tuple <AuthUserDto, string>(
                           new AuthUserDto
                {
                    UserId = user.Id,
                    Nickname = user.Nickname,
                    SchoolId = user.SchoolId,
                    UserTypeId = user.UserTypeId,
                    UserType = user.UserType.Name,
                },
                           null
                           ));
            }
        }