Exemplo n.º 1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,LastName,PhotoUrl,Email,PhoneNumber,BirthDate,StateId, myfriendId")] FriendCreateEditModel model,
                                              HttpPostedFileBase binaryFile, IEnumerable <Guid> myfriendId)
        {
            if (!ModelState.IsValid)
            {
                var tempApiBasePath = ApiBasePath;

                ApiBasePath = "https://assessmentcountrflagwebapi20190325105438.azurewebsites.net";

                ViewBag.CountryId = await ListBy <Country>("api/countries");

                ApiBasePath = tempApiBasePath;

                var friends = (await ListBy <Friend>("api/friends")).Select(f => new { MyFriendId = f.Id, FriendFullName = $"{f.Name} {f.LastName}" });

                ViewBag.MyFriends = new MultiSelectList(friends, "MyFriendId", "FriendFullName", (List <Guid>)Session["friendList"]);

                return(View(model));
            }

            if (binaryFile != null)
            {
                model.PhotoUrl = await UploadPhotoAsync(binaryFile, ContainerName.Profile);
            }

            model.FriendsGuidList = myfriendId.ToList();

            var friend = JsonConvert.SerializeObject(model);

            await Put($"{UrlBase}/{model.Id}", friend);

            Session["friendList"] = null;

            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> PostFriend(FriendCreateEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var state = await db.States.FirstOrDefaultAsync(s => s.Id == model.StateId);

            if (state == null)
            {
                return(BadRequest("state invalid"));
            }
            var friend = new Friend
            {
                Id          = Guid.NewGuid(),
                Name        = model.Name,
                LastName    = model.LastName,
                PhotoUrl    = model.PhotoUrl,
                PhoneNumber = model.PhoneNumber,
                Email       = model.Email,
                State       = state,
                BirthDate   = model.BirthDate,
            };

            try
            {
                db.Friends.Add(friend);

                await db.SaveChangesAsync();

                foreach (var friendId in model.FriendsGuidList)
                {
                    var query = $"insert into [Friendship] values('{friend.Id}','{friendId}')";

                    await db.Database.ExecuteSqlCommandAsync(query);
                }
            }

            catch (Exception)
            {
                if (FriendExists(friend.Id))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("DefaultApi", new { id = friend.Id }, friend));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> PutFriend(Guid id, FriendCreateEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != model.Id)
            {
                return(BadRequest());
            }

            var state = await db.States.FirstOrDefaultAsync(s => s.Id == model.StateId);

            if (state == null)
            {
                return(BadRequest("state invalid"));
            }

            var friend = new Friend
            {
                Id          = model.Id,
                Name        = model.Name,
                LastName    = model.LastName,
                PhotoUrl    = model.PhotoUrl,
                PhoneNumber = model.PhoneNumber,
                Email       = model.Email,
                State       = state,
                BirthDate   = model.BirthDate
            };



            try
            {
                db.Entry(friend).State = EntityState.Modified;

                await db.SaveChangesAsync();

                var query         = $"select ChildFriendId from [Friendship] where MainFriendId = '{model.Id}'";
                var dbFriendships = db.Database.SqlQuery <Guid>(query).ToList();

                foreach (var item in model.FriendsGuidList)
                {
                    if (!dbFriendships.Any(f => f.Equals(item)))
                    {
                        await db.Database.ExecuteSqlCommandAsync($"insert into [Friendship] values('{model.Id}','{item}')");
                    }
                }

                foreach (var item in dbFriendships)
                {
                    if (!model.FriendsGuidList.Any(f => f.Equals(item)))
                    {
                        await db.Database.ExecuteSqlCommandAsync($"DELETE from [Friendship] where (MainFriendId = '{model.Id}' AND ChildFriendId = '{item}')");
                    }
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FriendExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }