コード例 #1
0
ファイル: AnimalController.cs プロジェクト: nirmana/PetWhizz
        public PetWhizzResponse SharePet(PetShareRequest PetShareRequest)
        {
            PetWhizzResponse _oResponse;

            try
            {
                animalService.SharePet(PetShareRequest);
                _oResponse = Utils.CreateSuccessResponse(null);
            }
            catch (Exception ex)
            {
                _oResponse = Utils.CreateErrorResponse(ex);
            }
            return(_oResponse);
        }
コード例 #2
0
ファイル: AnimalService.cs プロジェクト: nirmana/PetWhizz
        internal void SharePet(PetShareRequest petShareRequest)
        {
            logger.Trace("Recived Pet share request");
            try
            {
                if (String.IsNullOrEmpty(petShareRequest.username) || String.IsNullOrEmpty(petShareRequest.email) || petShareRequest.petId <= 0)
                {
                    logger.Error("Recived Pet share request");
                    throw new CustomException("Pet share request dosent have required fields", (int)ErrorCode.VALIDATIONFAILED);
                }
                CurrentUser currentUser = (CurrentUser)HttpContext.Current.User;
                using (var ctx = new PetWhizzEntities())
                {
                    user User = ctx.users.Where(a => a.eMail.ToLower() == petShareRequest.email.ToLower()).FirstOrDefault();
                    if (User == null)
                    {
                        logger.Error("Requested user details not matching for Petwhizz user email - " + petShareRequest.email);
                        throw new CustomException("Requested user details not matching for Petwhizz user", (int)ErrorCode.USERNOTFOUND);
                    }
                    if (User.userName.ToLower() != petShareRequest.username.ToLower())
                    {
                        logger.Error("Requested user details not matching for Petwhizz user username - " + petShareRequest.username);
                        throw new CustomException("Requested user details not matching for Petwhizz user username", (int)ErrorCode.VALIDATIONFAILED);
                    }
                    pet Pet = ctx.pets.Where(a => a.id == petShareRequest.petId).FirstOrDefault();
                    if (Pet.petOwners.Where(a => a.userId == currentUser.userId && a.isActive == true && a.isMainOwner == true).FirstOrDefault() == null)
                    {
                        logger.Error("Requested user not belongs to the pet or not the main owner");
                        throw new CustomException("Requested user not belongs to the pet or not the main owner", (int)ErrorCode.UNAUTHORIZED);
                    }

                    petOwner PetOwner = ctx.petOwners.Where(a => a.petId == petShareRequest.petId && a.userId == User.id).FirstOrDefault();
                    if (PetOwner != null && PetOwner.isActive == true)
                    {
                        logger.Error("Pet is already shared with requestd user");
                        throw new CustomException("Pet is already shared with requestd user", (int)ErrorCode.ALREADYEXIST);
                    }
                    else if (PetOwner != null && PetOwner.isActive == false)
                    {
                        logger.Error("Pet is already shared with this user but not yet confirmed.");
                        throw new CustomException("Pet is already shared with this user but not yet confirmed.", (int)ErrorCode.NOTCONFIRMED);
                    }
                    else
                    {
                        //share pet
                        PetOwner = new petOwner()
                        {
                            enteryDate   = DateTime.Now,
                            isActive     = false,
                            isMainOwner  = false,
                            petId        = petShareRequest.petId,
                            sharedTime   = DateTime.Now,
                            sharedUserId = currentUser.userId,
                            acceptedTime = null,
                            userId       = User.id
                        };
                        ctx.petOwners.Add(PetOwner);
                        ctx.SaveChanges();
                        // var EmailService = new EmailService();
                        SendPetSharingEmail(PetOwner);
                    }
                }
            }
            catch (CustomException) { throw; }
            catch (Exception ex)
            {
                logger.Error(MethodBase.GetCurrentMethod().Name + ": exception: " + ex.Message + ", " + ex.InnerException);
                throw new CustomException("SystemError", ex, (int)ErrorCode.PROCEESINGERROR);
            }
        }