Example #1
0
        public async Task <ActionResult <UserToken> > PostUserToken([FromRoute] int id) // #1
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            UserToken userToken = new UserToken()
            {
                UserID      = id,
                Expiry      = DateTime.UtcNow.Add(new TimeSpan(0, 30, 0)),
                TokenIsUsed = false,
                Token       = KeyGenerator.GetUniqueKeyMixed(5),
                Email       = (await GetUserFromUserTokenIDAsync(id)).Email
            };

            userTokenRepository.Add(userToken);

            await uoW.SaveAsync();


            emailConfig.Details(
                from: "*****@*****.**",
                to: userToken.Email,
                fromPassword: "******"
                );
            emailConfig.EmailManager
            .SendMessage("Forgot Password Token!", userToken.Token);     // sends the token by email

            return(CreatedAtRoute("DefaultApi", new { id = userToken.ID }, userToken));
        }
        public async Task <IHttpActionResult> PostUserToken(int id) // using int id better than using Dto
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //var userToken = DtoToEntityIMapper.Map<UserTokenDto, UserToken>(userTokenDto); ////

            UserToken userToken = new UserToken()
            {
                UserID      = id,
                Expiry      = DateTime.UtcNow.Add(new TimeSpan(0, 30, 0)),
                TokenIsUsed = false,
                Token       = KeyGenerator.GetUniqueKeyMixed(5),
                Email       = (await GetUserFromUserTokenIDAsync(id)).Email
            };

            UoW.GetRepository <UserToken>().Insert(userToken);

            await UoW.SaveAsync();


            emailConfig.Details(
                from: "*****@*****.**",
                to: userToken.Email,
                fromPassword: "******"
                );
            emailConfig.EmailManager
            .SendMessage("Forgot Password Token!", userToken.Token);     // sends the token by email

            return(CreatedAtRoute("DefaultApi", new { id = userToken.ID }, userToken));
        }
Example #3
0
        [ResponseType(typeof(void))] // NEEDS CHANGING: WE CANT MODIFY EMAIL ADDRESS OF THE USER
        public async Task <IHttpActionResult> PutUser(int id, UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != userDto.ID)
            {
                return(BadRequest());
            }
            if (userDto.Email !=
                UoW.GetRepository <User>().GetItem(e => e.ID == id).Email)
            // meaning we cant update the email. can be improved?
            {
                return(BadRequest());
            }

            if (userDto.Password.Count() < 6)
            {
                return(BadRequest("Password should at least 6 characters"));
            }

            var user = DtoToEntityIMapper.Map <UserDto, User>(userDto);

            UoW.GetRepository <User>().ModifyEntityState(user);  // update happens here

            try
            {
                await UoW.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            // send success email***
            emailConfig.Details(
                from: "*****@*****.**",
                to: userDto.Email,
                fromPassword: "******"
                );
            emailConfig.EmailManager.SendMessage("User info update",
                                                 "User info was successfully updated");

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #4
0
        public async Task <ActionResult <UserDto> > PutUser([FromRoute] int id, [FromBody] UserDto userDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != userDto.ID)
            {
                return(BadRequest());
            }
            if (userDto.Email !=
                repository.Get(e => e.ID == id).Email)

            {
                return(BadRequest());
            }

            if (userDto.Password.Count() < 6)
            {
                return(BadRequest("Password should at least 6 characters"));
            }

            User user = DtoToEntityIMapper.Map <UserDto, User>(userDto);

            repository.ModifyEntryState(user, EntityState.Modified);

            try
            {
                await uoW.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            // send success email***
            emailConfig.Details(
                from: "*****@*****.**",
                to: userDto.Email,
                fromPassword: "******"
                );
            emailConfig.EmailManager.SendMessage("User info update",
                                                 "User info was successfully updated");


            return(NoContent());
        }
Example #5
0
        public async Task <IHttpActionResult> PostOnlineParticipationRequest(OnlineParticipationRequestDto onlineParticipationRequestDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var onlineParticipationRequest = DtoToEntityIMapper
                                             .Map <OnlineParticipationRequestDto, OnlineParticipationRequest>(onlineParticipationRequestDto); ////

            string countryCode = (await UoW.GetRepository <Country>()
                                  .GetItemAsycn(e => e.ID == onlineParticipationRequestDto.CountryID))
                                 .CountryCode;

            onlineParticipationRequest.TrackingCode = GenerateTrackingCode(countryCode);

            UoW.GetRepository <OnlineParticipationRequest>().Insert(onlineParticipationRequest);

            await UoW.SaveAsync();

            string toEmail = (await UoW.GetRepository <User>()
                              .GetItemAsycn(e => e.ID == onlineParticipationRequestDto.UserID))
                             .Email;

            emailConfig.Details(from: "*****@*****.**",
                                to: toEmail,
                                fromPassword: "******"
                                );

            emailConfig.EmailManager.SendMessage("Online Participation form",
                                                 "You have successfully participated in the online form");

            return(CreatedAtRoute("DefaultApi", new { id = onlineParticipationRequest.ID }, onlineParticipationRequest));
        }
        public async Task <ActionResult <OnlineParticipationRequestDto> > PostOnlineParticipationRequest([FromBody] OnlineParticipationRequestDto onlineParticipationRequestDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            OnlineParticipationRequest onlineParticipationRequest = DtoToEntityIMapper.Map <OnlineParticipationRequestDto, OnlineParticipationRequest>(onlineParticipationRequestDto);

            repository.Add(onlineParticipationRequest);
            await uoW.SaveAsync();

            string toEmail = (await uoW.GetRepository <User>()
                              .GetAsync(e => e.ID == onlineParticipationRequestDto.UserID))
                             .Email;

            emailConfig.Details(from: "*****@*****.**",
                                to: toEmail,
                                fromPassword: "******"
                                );

            emailConfig.EmailManager.SendMessage("Online Participation form",
                                                 "You have successfully participated in the online form");

            return(CreatedAtAction("GetOnlineParticipationRequest", new { id = onlineParticipationRequest.ID }, onlineParticipationRequestDto));
        }